List all the tables from all the databases without cursor

  • I want to list all the tables from all the databases in a select query, without using any cursor.

    Is it possible????????

    Thanks

  • you can get the details from sys.objects or by running sp_tables.

    select * from sys.objects where type = 'U

    Regards,

    Sriram

    Sriram

  • You can use an undocumented stored procedure sp_msforeachdb:

    EXEC sp_msforeachdb 'select * from sys.tables'

    or, if you need one resulset:

    select Replicate(' ', 128) DB, * into #temp from sys.tables

    delete #temp

    exec sp_msforeachdb 'use [?] insert #temp select ''?'', * from sys.tables '

    select * from #temp

    drop table #temp

  • sp_msforeachdb is a cursor.

    You can use the INFORMATION_SCHEMA system views. Specifically INFORMATION_SCHEMA.TABLES. These are common between SQL Server 2000, 2005 and 2008, so are a much better place to program against than sys.objects, which changes a bit (and between 2000 & 2005, more than a bit) between all three versions.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Vic.K (2/11/2009)


    You can use an undocumented stored procedure sp_msforeachdb:

    EXEC sp_msforeachdb 'select * from sys.tables'

    or, if you need one resulset:

    select Replicate(' ', 128) DB, * into #temp from sys.tables

    delete #temp

    exec sp_msforeachdb 'use [?] insert #temp select ''?'', * from sys.tables '

    select * from #temp

    drop table #temp

    How about using

    select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME

    from information_schema.tables

    where table_type = 'BASE TABLE'

    order by table_schema, table_name

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • ALZDBA (2/11/2009)

    How about using

    select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME

    from information_schema.tables

    where table_type = 'BASE TABLE'

    order by table_schema, table_name

    But it's good only for the actual database, isn't it? So, there is no possibility to list all tables from all databases without cursors?

  • You could always union the same statement for each database.

    SELECT *

    FROM DB1.sys.sysobjects

    UNION

    SELECT *

    FROM DB2.sys.sysobjects

  • Vic is correct. because there is no single repository for the tables/objects of all databases, you have to query each repository seperately...whether information_schema, sysobjects or sys.objects, you gotta do each seperately.

    this is a situation where a cursor is a good thing in order to accomplish the task at hand.

    Vic.K (2/11/2009)

    But it's good only for the actual database, isn't it? So, there is no possibility to list all tables from all databases without cursors?

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Vic.K (2/11/2009)


    ALZDBA (2/11/2009)

    How about using

    select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME

    from information_schema.tables

    where table_type = 'BASE TABLE'

    order by table_schema, table_name

    But it's good only for the actual database, isn't it? So, there is no possibility to list all tables from all databases without cursors?

    Indeed, you still have to qualify the database or run the query _in_ the database.

    select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME

    from mydb.information_schema.tables

    where table_type = 'BASE TABLE'

    order by table_schema, table_name

    you might as well generate the whole sqlstatement to execute....

    Declare @SQLStmt varchar(max)

    Set @SQLStmt = ''

    Select @SQLStmt = @SQLStmt

    + case @SQLStmt when '' then '' else ' union all ' end

    + 'select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME

    from ' + name + '.information_schema.tables

    where table_type = ''BASE TABLE'' '

    from master.sys.databases;

    Print @SQLStmt

    exec ( @SQLStmt )

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • You rock ALZDBA....

    Its working like M16 ..........:cool:

    Thanks dude!!!

  • The old sp_tables works too 😀

  • Hi

    Yet another way

    [font="Courier New"]

    DECLARE @sql NVARCHAR(MAX)

    SET @sql = ''

    SELECT @sql = @sql + CASE WHEN LEN(@sql) != 0 THEN 'UNION ALL ' ELSE '' END + 'SELECT * FROM ' + QUOTENAME(name) + '.sys.tables' + CHAR(13) + CHAR(10)

       FROM sys.databases

       WHERE name NOT IN ('tempdb', 'master', 'msdb', 'model')

          --AND name LIKE 'Adventure%'

    EXECUTE sp_executesql @sql

    [/font]

    Greets

    Flo

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply