Multi database query - sql-server

The code example given in Query across multiple databases on same server will work for what I want to do. My only question is whether it's possible to include the name of the database in the SELECT statement for the records that are retrieved. In other words, I'd like to know if a record is from DB1 or DB2. Is this possible?

This answer is based on the code from the accepted answer in the linked question (link).
Since you know while creating the View from which database you are pulling the data, you can add this information as a static column:
CREATE VIEW vCombinedRecords AS
SELECT 'DB1' as Database, * FROM DB1.dbo.MyTable
UNION ALL
SELECT 'DB2', * FROM DB2.dbo.MyTable
This will tag every record from DB1 with the value "DB1" in the Database column

Sure, try:
CREATE VIEW vCombinedRecords AS
SELECT 'DB1' as Database_Name,* FROM DB1.dbo.MyTable
UNION ALL
SELECT 'DB2',* FROM DB2.dbo.MyTable

Related

tsql: select view from different database

Is it possible to select view defined in different database in MS SQL Server?
All my searching results point to defining view to use data from different database, but haven't found if it possible to select view from another database yet.
suppose you want to do a select on database DBOther than it would be :
select * from DBOther..TableName
Also check if the table or view is on the dbo schema, if not you should add the schema also : Please notice I use only one dot now after the database name
select * from DBOther.dbo.ViewName
Make sure the Database is in the Linked Server if they are not on the same server.
Then you can access the table or view on that database via:
SELECT * FROM [AnotherServerName].[DB].[dbo].[Table]
If on same server:
SELECT * FROM [DB].[dbo].[Table]

SQL Server select from non-existent table

I have a query in classic asp where SQL statement is this:
Select * from active_Case
I verified in the DB connection it is using and found there is no such table / view. But a table does exist by the name of Cases. Internally it appears to be selecting from this table itself.
Actually this is somebody else's code. Thus I am not sure how is it possible. Is it really possible or am I missing something?
this will give you the base table name
select name, base_object_name
from sys.synonyms
where name = 'active_Case'
other than tables and view you can even check in User defined table type under types. or there might be chance your table is having a schema other than 'dbo.'

How to list all Informix database names

I want to know if there is any way to list (get) all the database names on Informix. I need a proper query or stored procedure to do that.
Alternatively you could execute this query when connected to the sysmaster database:
select * from sysdatabases
More to the point (years after the question was posed):
select name from sysmaster:sysdatabases;
According to the documentation, the command to get a list of the databases on the server you are connected to is simply:
show databases

How to do a union across multiple instances of Sql Server?

I have two Sql Servers (two distinct databases, (i.e. two machines)) with the same structure. Is there a way to do a SELECT * FROM (SELECT * FROM TableOnServerA Union SELECT * FROM TableOnServerB)?
Thanks
Yes, just set them up as linked servers and then fully qualify the names in the form of LinkName.DatabaseName.SchemaName(dbo).TableName
SELECT * FROM serverA.database.owner.TableName
Union
SELECT * FROM serverB.database.owner.Tablename
assuming that they are setup as linked server, use books online and go to "linked"
You could create a Linked Server or use OPENROWSET to connect to other SQL database.
You can run a query relating two different machines adding one machine via the sp_addlinkedserver stored procedure. You run it in the database server instance where you will want to execute the query (or on both if you want to execute the query in any server), like this
use master
go
exec sp_addlinkedserver
#server='AnotherServer',
#provider='SQL Server'
The you can run
select * from AnotherServer.database.dbo.table t1
join database.dbo.table t2 on (t1.id = t2.id)
Incidentally if they are on two different servers and you are sure the data is not duplicated use union all, it will be much faster. Also never use select *,specify the field names. Other wise it will break if someone adds a column to A but not to B (or rearranges teh columns in B but not A) I would also add a column indicating which of the two servers the data came from, especially if they might have the same id number but attached to different data (which can happen if you are using autogenerated ids). This can save no end of trouble.

How do you show a list of tables in another database?

I can use:
select * from sys.tables
in mssql to show a list of all tables in the current database. Is there anyways I can use similar syntax to show list of tables in another database?
Say I am using A with:
use A
statement, can I show tables in database B?
This does it for me (MS SQL 2005 and newer):
select * from your_database_name.sys.tables
Keep in mind that you (or whatever authentication context you're using) will still need read permission on that database.
To use your example:
use a;
go
select * from sys.tables; -- selects table info from a
select * from b.sys.tables; -- selects table info from b
Another possibility is to use:
select * from your_database_name.information_schema.tables

Resources