Results 1 to 11 of 11

Thread: Query to get Table Names by specifying database

  1. #1

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Query to get Table Names by specifying database

    Hi all,

    I am trying to write a query for a stored procedure that will allow me to pass in the database name and it will return a list of tables in that database. I know I can do a exec sp_tables but I will not know which database I need to execute this on. Essentially I want something like this: select * from [specifryDBhere].sysobjects where xtype IN ('U', 'S', 'V') order by name asc

    Is there a table in master that lists all tables in all databases? How else can I do this in the stored procedure?

    Thanks for any assistance!

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Query to get Table Names by specifying database

    This link shows a post

    http://www.vbforums.com/showpost.php...67&postcount=8

    that loops through the sysobjects table looking for indexes, views and procedures.

    We use it to DROP certain items before sending a DB to a data warehouse.

    The tables should show up in sysobjects also with a certain "type" - run a loop on your database (without the DROPS of course) and see what you get.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Query to get Table Names by specifying database

    Actually this select appears to do the trick as well.

    SELECT * FROM SYSOBJECTS WHERE TYPE='U'

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Re: Query to get Table Names by specifying database

    Yes, that works but for whatever database I am currently in. But I want to specify what database to look in so I get the tables in that database. Is there a master listing of tables that are in all databases on the server or can I somehow specify as a parameter to my stored proc which database to do the query against?

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Query to get Table Names by specifying database

    The objects are stored in the DB only (AFAIK).

    I usually suggest not to use DYNAMIC SQL - but I cannot think of a way around it...

    Code:
    USE ACCTFILES
    GO
    DROP PROCEDURE SHOWTABLES
    GO
    CREATE PROCEDURE SHOWTABLES @DB VARCHAR(100) AS
    BEGIN
    	DECLARE @SQL VARCHAR(100)
    	SET @SQL='SELECT * FROM ['+@DB+'].[DBO].[SYSOBJECTS] WHERE TYPE=''U'''
    	EXEC (@SQL)
    END
    GO
    EXEC SHOWTABLES 'ACCTFILES'
    EXEC SHOWTABLES 'FUNDS'

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Re: Query to get Table Names by specifying database

    Thanks, I will give it a shot.

  7. #7

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Re: Query to get Table Names by specifying database

    Yup,

    That is exactly what I was looking for - would have taken me a long long time to figure out the syntax - thanks very much for you assistance sz!

  8. #8

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    Re: Query to get Table Names by specifying database

    One more question - can't seem to figure out how to make the U in the query us a passed in parameter (I have never done dynamic sql before...)

    This does not work - can't seem to get the syntax correct near the @OBJTYPE:

    DECLARE @SQL VARCHAR(100)
    DECLARE @OBJTYPE VARCHAR(1)
    DECLARE @DB VARCHAR(100)
    SET @DB = 'DNNameHere'
    SET @OBJTYPE = 'U'

    SET @SQL='SELECT * FROM ['+@DB+'].[DBO].[SYSOBJECTS] WHERE TYPE='@OBJTYPE''
    EXEC (@SQL)

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Query to get Table Names by specifying database

    Just do the same as was done for @DB, ie:
    SET @SQL='SELECT * FROM ['+@DB+'].[DBO].[SYSOBJECTS] WHERE TYPE=' + @OBJTYPE + ''

    ..but as the + '' at the end doesn't do anything, you can do this:
    SET @SQL='SELECT * FROM ['+@DB+'].[DBO].[SYSOBJECTS] WHERE TYPE=' + @OBJTYPE

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Query to get Table Names by specifying database

    I think something like this?
    SET @SQL='SELECT * FROM ['+@DB+'].[DBO].[SYSOBJECTS] WHERE TYPE=''+@OBJTYPE+'''

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Query to get Table Names by specifying database

    Good point - I forgot about the quotes around the text value

    You need to double-up on the quotes inside a string tho, eg:

    SET @SQL='SELECT * FROM ['+@DB+'].[DBO].[SYSOBJECTS] WHERE TYPE='''+@OBJTYPE+''''

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width