Specify database name in an SQL query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dennison
    New Member
    • Oct 2007
    • 6

    Specify database name in an SQL query

    I need to specify the database name dynamically. I thought I can get away with doing something like "select * from [db_name].[table_name]" but apparently MS SQL does not work that way. Is there another way to specify the database name in an SQL query?

    I could also probably switch databases with a "use" statement, but how do I do this with a SqlConnection?
  • dennison
    New Member
    • Oct 2007
    • 6

    #2
    Okay I just found out that it works if I use [db_name].dbo.[table_name]

    Why oh why must Microsoft insist on their own non-standard ways????

    Comment

    • bendover
      New Member
      • Sep 2013
      • 1

      #3
      First off, what is standard? Second, it is T-SQL, a standard!

      Originally posted by dennison
      Okay I just found out that it works if I use [db_name].dbo.[table_name]

      Why oh why must Microsoft insist on their own non-standard ways????

      It's actually you that is not standard. Microsoft's T-SQL engine was originally Sybase's. So you are the one in the wrong, and it goes SERVER.DATABASE .OWNER.OBJECT which is pretty straightforward if you think about it.

      Sorry you had to go on Google for 2 minutes to find that :)

      Comment

      Working...