Create view across multiple databases - sql-server

I have two databases; 1 is a live database for daily data input and the other is an archival DB for older data.
How can I create a view which gets data from both databases?
Three tables are involve... database1.dbo.table and database1.dbo.tran1 in same database, and database_archived.dbo.table1:
Create VIEW [dbo].[VW_Table_ALL]
AS
SELECT * FROM database1.dbo.table1
UNION ALL
SELECT * FROM database_archived.dbo.table1 as Data INNER JOIN
database1.dbo.tran1 as Tran ON Data.Tran_id = Tran.Tran_Id
GO

Not sure if you need a UNION or a JOIN, but in either case you can just use a three-part name for the object in the other database:
USE database1;
GO
CREATE VIEW dbo.MyView
AS
SELECT columns FROM dbo.LocalTable
UNION ALL
SELECT columns FROM database2.dbo.RemoteTable;
GO

Related

how to create a view using two tables in different databases in ibm db2

I have got two tables T1 and T2 . T1 is present in database D2 and T2 is present in database D2. I am using DB2 database. I also have two queries for fetching data from T1 and T2 respectively.
Now, I have a requirement where I need to create a view and get the combined data of two queries in that view.
For example, create view testview as select employeeName, employeeGender from sampledb.employee where employeeID >6 UNION select studentName, studentGender from testdb2.student where studentId > 6.
I want to do something like this.
I have searched a lot on internet for creating a view on different schemas in DB2, but couldn't get a right solution.
Please help.
If you speak schema and not database, this doesn't work?
create view t1.viewname (name, gender) as
select employeeName, employeeGender
from S1.employee
where employeeID >6
UNION
select studentName, studentGender
from S2.student
where studentId > 6

SQL Server 2012 - How to dynamically select data from a view based on user privilleges

I am creating a security layer on few databases. As part of that I have to create a view in Original_Database that joins two tables (union) from different databases, Chained_Database1 and Chained_Database2.
USE Original_Database;
GO
CREATE VIEW QueryATable
AS
SELECT * FROM Chained_Database1.dbo.ATable
UNION
SELECT * FROM Chained_Database2.dbo.ATable;
GO
If user usergroupA (Usergroup) logs in and selects from the view QueryATable from Original_Database then he should be able to see only data from Chained_Database1.dbo.ATable.
Likewise if usergroupB logs in and selects from the view QueryATable, then he should be able to see only data from Chained_Database2.dbo.ATable.
I have tried giving different permissions to the users. It's either denying view access or giving result from two tables.
Your help is highly appreciated.
Thanks in advance!
Based on your requirements, the UNION is pointless and would only slow performance since you would select from both data sets... and then explicitly limit it to one. Theoretically you'd have a look up table for the user who ran the query, and you'd limit the results in the WHERE clause since you can't user parameters in a view. So, don't do the union. Instead create two separate views.
CREATE VIEW QueryATable
AS
SELECT * FROM Chained_Database1.dbo.ATable
GRANT SELECT ON QueryATable TO <who ever...>
GO
CREATE VIEW QueryBTable
AS
SELECT * FROM Chained_Database2.dbo.ATable
GRANT SELECT ON QueryBTable TO <who ever...>
GO
If you don't want to create separate views then you could handle this in a STORED PROCEDURE or some other way perhaps...
CREATE PROCEDURE yourProcedure(#userID)
AS
IF 1 = (SELECT COUNT(*) FROM LookUpTableA WHERE userID = #userID)
SELECT * FROM Chained_Database1.dbo.ATable
IF 1 = (SELECT COUNT(*) FROM LookUpTableB WHERE userID = #userID)
SELECT * FROM Chained_Database2.dbo.ATable
ELSE
SELECT 'User Not Valid'
GO
Note that this assumes the user would only be in one of the look up tables. If they were in both you'd need to handle it differently.

how to get a of list schemas in specific database in DB2

how to get a of list schemas in specific database in DB2.
I am trying to get list of specific schemas in particular database
Try this:
select schemaname from syscat.schemata
The catalog view SYSCAT.SCHEMATA holds the schema information. Here is the overview of all DB2 LUW catalog views in case you need other information as well.
The DB2 zOS schemas can be found with this query
select distinct schemaname
from (
select creator from sysibm.systables
union all
select schema from sysibm.sysdatatypes
union all
select schema from sysibm.sysroutines
union all
select schema from sysibm.systriggers
) schemata(schemaname)

How to get a list of all tables in two different databases

I'm trying to create a little SQL script (in SQL Server Management Studio) to get a list of all tables in two different databases. The goal is to find out which tables exist in both databases and which ones only exist in one of them.
I have found various scripts on SO to list all the tables of one database, but so far I wasn't able to get a list of tables of multiple databases.
So: is there a way to query SQL Server for all tables in a specific database, e.g. SELECT * FROM ... WHERE databaseName='first_db' so that I can join this with the result for another database?
SELECT * FROM database1.INFORMATION_SCHEMA.TABLES
UNION ALL
SELECT * FROM database2.INFORMATION_SCHEMA.TABLES
UPDATE
In order to compare the two lists, you can use FULL OUTER JOIN, which will show you the tables that are present in both databases as well as those that are only present in one of them:
SELECT *
FROM database1.INFORMATION_SCHEMA.TABLES db1
FULL JOIN database2.INFORMATION_SCHEMA.TABLES db2
ON db1.TABLE_NAME = db2.TABLE_NAME
ORDER BY COALESCE(db1.TABLE_NAME, db2.TABLE_NAME)
You can also add WHERE db1.TABLE_NAME IS NULL OR db2.TABLE_NAME IS NULL to see only the differences between the databases.
As far as I know, you can only query tables for the active database. But you could store them in a temporary table, and join the result:
use db1
insert #TableList select (...) from sys.tables
use db2
insert #TableList2 select (...) from sys.tables
select * from #TableList tl1 join Tablelist2 tl2 on ...
Just for completeness, this is the query I finally used (based on Andriy M's answer):
SELECT * FROM DB1.INFORMATION_SCHEMA.Tables db1
LEFT OUTER JOIN DB2.INFORMATION_SCHEMA.Tables db2
ON db1.TABLE_NAME = db2.TABLE_NAME
ORDER BY db1.TABLE_NAME
To find out which tables exist in db2, but not in db1, replace the LEFT OUTER JOIN with a RIGHT OUTER JOIN.

Relation between tables in different databases using SQL Server 2005

is there any way to add a relation between 2 tables in different databases ?
For example:
db1.dbo.table1 field id = db2.dbo.table2 field id
the nearest thing that i´m looking for is something like:
CREATE TRIGGER RELATIONAL on IDCLIENT
FOR INSERT
As
IF (SELECT COUNT(*) FROM table1) = 1
INSERT INTO db2.dbo.table2(ID)
SELECT ID FROM table1
You can create a synonym for the other table in another db, and then you can reference it by its synonym name.

Resources