PHP SQL Server Database Select

2019-07-24 21:06发布

问题:

I have three SQL Server databases on one SQL Server instance. Every time I do query to different databases, I have to select the database.

Is there a way to do query without database select. Like this:

SELECT * FROM database_name.table_name WHERE id = 1

回答1:

Yes, you can use identifiers as object names. This should work:

SELECT * 
  FROM database_name..table_name 
 WHERE id = 1

It's a variation of database_name.schema_name.object_name. There are more examples in the link.

Caveat


The user you use needs to have access to both the database(s) and table(s) involved.