Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Location
    PAKISTAN
    Posts
    106

    Unanswered: How to access tables from a dynamic

    I have a procedure that takes database name and queries some tables in the database specified. I am trying to access tables like this:

    SELECT COUNT(id) FROM @dbname..sysobjects WHERE type='U'

    How can I access the tables while the database name is in a variable.

    Thanx.

  2. #2
    Join Date
    Mar 2004
    Location
    Toronto
    Posts
    28
    Quote Originally Posted by TALAT
    I have a procedure that takes database name and queries some tables in the database specified. I am trying to access tables like this:

    SELECT COUNT(id) FROM @dbname..sysobjects WHERE type='U'

    How can I access the tables while the database name is in a variable.

    Thanx.
    You can do it with an exec call:

    EXEC('SELECT COUNT(id) FROM ' + @dbname + '..sysobjects WHERE type = ''U''')

    The U value needs to be surrounded by 2 pairs of single quotes, because it is a string within a string.

  3. #3
    Join Date
    Feb 2004
    Location
    PAKISTAN
    Posts
    106
    Hi, Thanx.

Posting Permissions

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