Search of table names
Asked Answered
S

11

127

I use the following to search for strings within the stored procedures of a specific database:

USE DBname
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%xxx%'

Is it easy to amend the above so that it searches Table names in a specific db "DBname" ?

Shealy answered 26/10, 2012 at 11:0 Comment(1)
See Also: How do I get list of all tables in a database using TSQL?Manouch
C
212

I'm using this and works fine

SELECT * FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME LIKE '%%'
Coypu answered 3/3, 2014 at 14:3 Comment(5)
Includes views too :-)It
Works for views and tables.Heber
use SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME LIKE '%%' for stored procedure.Heber
I suggest to use ILIKE instead of LIKE, to make the query case insensitiveResponser
LIKE is case-insensitive in SQL ServerGlauconite
H
92
select name
  from DBname.sys.tables
 where name like '%xxx%'
   and is_ms_shipped = 0; -- << comment out if you really want to see them
Hammack answered 26/10, 2012 at 11:3 Comment(3)
commenting out the line and is_ms_shipped = 0; doesn't seem to do muchShealy
The only thing it normally hides are the database diagram support tables, or is there only one. It's more useful when looking for functions/procedures.Hammack
Why not add schema names? select t.schema_id, s.name, t.Name from DBNAME.sys.tables as t inner join DBNAME.sys.schemas as s on t.schema_id = s.schema_id where t.name like '%yourtabletosearch%' and is_ms_shipped = 0Almost
B
16

If you want to look in all tables in all Databases server-wide and get output you can make use of the undocumented sp_MSforeachdb procedure:

sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like ''%Table_Names%'''
Benavides answered 30/12, 2017 at 18:6 Comment(1)
This answer is the only useful one if you don't know which database a table might be inDispassion
T
13

You can also use the Filter button to filter tables with a certain string in it. You can do the same with stored procedures and views.

enter image description here

Tonus answered 21/3, 2019 at 17:4 Comment(0)
B
7

I am assuming you want to pass the database name as a parameter and not just run:

SELECT  *
FROM    DBName.sys.tables
WHERE   Name LIKE '%XXX%'

If so, you could use dynamic SQL to add the dbname to the query:

DECLARE @DBName NVARCHAR(200) = 'YourDBName',
        @TableName NVARCHAR(200) = 'SomeString';

IF NOT EXISTS (SELECT 1 FROM master.sys.databases WHERE Name = @DBName)
    BEGIN
        PRINT 'DATABASE NOT FOUND';
        RETURN;
    END;

DECLARE @SQL NVARCHAR(MAX) = '  SELECT  Name
                                FROM    ' + QUOTENAME(@DBName) + '.sys.tables
                                WHERE   Name LIKE ''%'' + @Table + ''%''';

EXECUTE SP_EXECUTESQL @SQL, N'@Table NVARCHAR(200)', @TableName;
Baldachin answered 26/10, 2012 at 11:7 Comment(0)
M
7

If you prefer case-insensitive searching:

SELECT * FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME ILIKE '%%'

or

SELECT * FROM INFORMATION_SCHEMA.TABLES 
WHERE Lower(TABLE_NAME) LIKE Lower('%%')
Mirella answered 29/11, 2018 at 11:25 Comment(0)
D
3

Adding on to @[RichardTheKiwi]'s answer.

Whenever I search for a list of tables, in general I want to select from all of them or delete them. Below is a script that generates those scripts for you.

The generated select script also adds a tableName column so you know what table you're looking at:

select 'select ''' + name + ''' as TableName, * from ' + name as SelectTable,
'delete from ' + name as DeleteTable
from sys.tables
where name like '%xxxx%'
and is_ms_shipped = 0; 
Duthie answered 29/3, 2016 at 9:7 Comment(0)
I
2

you can also use the show command.

show tables like '%tableName%'
Ironstone answered 8/2, 2017 at 13:22 Comment(1)
This is MySQL syntax on a question tagged sql-serverPhenice
W
2

I want to post a simple solution for every schema you've got. If you are using MySQL DB, you can simply get from your schema all the table's name and add the WHERE-LIKE condition on it. You also could do it with the usual command line as follows:

SHOW TABLES WHERE tables_in_<your_shcema_name> LIKE '%<table_partial_name>%';

where tables_in_<your_shcema_name> returns the column's name of SHOW TABLES command.

Weisberg answered 11/2, 2019 at 11:43 Comment(0)
O
2

You can use below :

Select * from sys.tables where name like '%yourtablename%'
Orest answered 25/4, 2022 at 14:18 Comment(0)
D
1

This will working fine....

SELECT * FROM sys.TABLES WHERE name LIKE '%%'

Dahabeah answered 25/11, 2022 at 9:28 Comment(1)
Welcome to Stackoverflow. This question is asked more than 10 years ago and it has an accepted answer. Please add some details about the reason you are adding a new answer.Hemato

© 2022 - 2024 — McMap. All rights reserved.