How can I find a table in SQL Server 2016 - sql

How can I find a table in SQL Server 2016?
I could use this to find the table in the current database:
SELECT *
FROM sys.Tables
WHERE name LIKE '%App_Current_Seq_Num%'
The problem is, I have several databases on the server.

Please try with the below sql query.
select * from [YourDatabaseName].INFORMATION_SCHEMA.TABLES where TABLE_NAME LIKE '%App_Current_Seq_Num%'

sp_tables , sys.tables , information_schema.tables will only give you the list of tables from current selected database. Use the below undocumented stored procedure to run this code for each database, with each resultset showing the list of tables belonging to it.
sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like ''%App_Current_Seq_Num%'''
Another option is to use dynamic SQL .

You can either do
use your_database_name
SELECT *
FROM sys.Tables
WHERE name LIKE '%App_Current_Seq_Num%'
or
SELECT *
FROM your_database_name.sys.Tables
WHERE name LIKE '%App_Current_Seq_Num%'
or if you want to search multiple databases at once use a union all clause.
Something like this:
SELECT * FROM database1.sys.Tables
UNION ALL
SELECT * FROM database2.sys.Tables

I used this and it worked perfectly:
USE Master
GO
EXEC sp_MSforeachdb
#command1='use ?; SELECT *
FROM sys.tables
WHERE name LIKE ''%App_Current_Seq_Num%'''

you can use SQL Search
it is a simple search control that you can use in sql server
to find tables/views/stored procedures and etc...
use fast free download from
https://www.red-gate.com/products/sql-development/sql-search/index

Related

Get current table name in query

Is it possible to get table name in select statement?
Example:
SELECT Id, "Users" as TableName
FROM Users
I don't want to write "Users" manually but to determine it based on FROM statement.
I'm doing some migrations from one database to another (with different structure), and I have mapping table that stores old table names so that's why I need this (it's less error prone).
You can use following sub-query to get this information (works on SQL Server 2012+):
SELECT *,
(select object_name(ind.object_id)
from sys.fn_PhysLocCracker(%%physloc%%) plc
INNER JOIN SYS.DM_DB_DATABASE_PAGE_ALLOCATIONS(DB_ID(),null,null,null,null) ind
ON ind.allocated_page_file_id = plc.file_id
AND ind.allocated_page_page_id = plc.page_id) as table_name
FROM [your table]
It is painfully slow, caching on the side page allocation will speed things up.
Alternatively, if you want to do it for all tables, you can use following stored procedure:
USE [DB_NAME]
EXEC sp_msforeachtable 'SELECT Id, ''?'' AS [TableName] FROM ?'
Maybe it will be helpful for somebody :)

How do I search for a table in sql 2005?

I just created a table within a huge database and saved it. I've tried to refresh the database to see if my new table appears and I closed & reopened the Management Studio but don't see my new table. I was wondering if there is a way to search for my new created table? I'm using SQL Server 2005.
Thanks a lot.
Select * from sys.tables where name like '%tablename%'
You can try this:-
SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[table_name_here]')
or try this:-
USE YourDBName
GO
SELECT *
FROM sys.Tables
where name like '%tablename%'
GO
If table doesn't appear, check under different schema.
select * from sys.all_objects where name like '%tableName%' and type = 'U'

SQL Select list of tables in a database

I am using SQL Server 2005. I am trying to SELECT a list of tables in one of my database. Here is my structure of my SQL Server:
- <IP>(SQL Server 9.0 -userlogin)
- Databases
- Company
- Tables
- dbo.UserDB
- dbo.detailsDB
- dbo.goodsDB
I would like to retrieve the values of dbo.UserDB, dbo.detailsDB, dbo.goodsDB
But i do not know what is the exact sql query needed.
I have tried many ways like
SELECT * FROM userlogin.Tables; and
SELECT * FROM userlogin.Company.Tables;, but none of them works.
I have seen quite a few posts which suggests using show databases and show tables, but they don't seem to work as well.
Is it possible to select a list of table names in a database in the first place?
Thank you for any help in advance.
Thanks for the MSDNA link that #TomTom provided, I can now list my tables in my database.
However, I would want to list specific tables where TABLE_NAME contains "user".
How can I do it? I am trying on the following sql but it is not displaying result at the moment:
SELECT DISTINCT TABLE_NAME
FROM Company.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%"user"%';
GO
Try the iNFORMATION_SCHEMA.
http://msdn.microsoft.com/en-us/library/ms186778.aspx
They contain all the schema information you need.
Use the new sys system catalog in SQL Server 2005 and up:
SELECT Name
FROM sys.tables
WHERE is_ms_shipped = 0 -- only list *your* tables - not the system / MS table
You can read more about system catalog views and how to query them in the MSDN Books Online - and be warned - there's lots more to read and learn!
You could use this
Use ***database name***
SELECT *
FROM sys.tables
WHERE name like '%user%'
Sorry, have seen that #marc_s has provided the answer before me!
You can use INFORMATION_SCHEMA as told by #TomTom:
USE <<YOUR_DB_NAME>>
SELECT TABLE_SCHEMA + '.' + TABLE_NAME, *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_SCHEMA + '.' + TABLE_NAME
You can customize above query as per your requirements.
For your other question to find tables where the name contains 'user', execute the following statement:
USE <<YOUR_DB_NAME>>
Exec sp_tables '%user%'
Try:
SELECT *
from sysobjects
where type = 'U' AND
NAME LIKE '%user%'
GO

Sql Server 2005 Database table is not visible whereas stored procedure is visible

Sir,
I've a problem regarding Database.
My problem is I've Database script file.When I integtrated the script file in Database.I found all the stored procedure is visible.But tables are not visible.
I wrote the Command as follows:
select * from sysobjects where type='p'
Now all the tables is showing but fields are not.
I wrote the Command to get the fields for particular table_name targeted as
select * from table_name .
But a message is Coming like Invalid Object name as table_name.
Please help me out to find the fields of the table ...
You can use the following query to retrieve the columns of the sought-for table:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'your table name'
You can list all columns with:
select * from sys.columns

identify stored procedures and triggers that carries a particular string in sql server

In sql server 2005
I need to identify all the stored procedures and triggers where a particular table name is used
for ex: i want to search for "Table1"
Please advise
In SQL2005:
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(object_id) LIKE '%Table1%'
Or in SQL Server Management Studio right-click on the table and choose "View dependencies", but this wont find procs that reference Table1 using dynamic SQL.
More info: http://databases.aspfaq.com/database/how-do-i-find-a-stored-procedure-containing-text.html
Download SQL Digger - works a treat
http://www.sqldigger.com/
Try one of these:
select sysobjects.name
from syscolumns
left join sysobjects on sysobjects.id = syscolumns.id
where syscolumns.name like '%Table1%'
order by 1
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Table1%'
GO