I am using SQL SERVER 2012.
I have a stored procedure where i create a temporary table called RESULT which carries result-set of an inner-join.
SELECT column(s)
FROM [database1].[table1]
INNERJOIN
[database2].[table2]
The result-set is processed from tables in DB SERVER 1.
Now,I have to insert the result set inside another table present in DB SERVER 2.
Select * from [server2].[Table1].dbo.User WHERE UserID = Result.UserID
How to access the database of Server2 table from Server1 table?
I would say the easiest option would be to use a linked server (https://msdn.microsoft.com/en-au/library/ff772782.aspx)
Then you can query the server using
SELECT *
FROM [SERVER].[DATABASE].[SCHEMA].[TABLE] t1
INNER JOIN [DATABASE2].[TABLE2] t2 ON t1.id = t2.id
Related
I have a task to create a stored procedure in Oracle DB. Given two different databases DB1 with student_lookup table and DB2 with student_master table . The SP needs to check if DB2.student_master's record exists in DB1.student_lookup table.
If the record exists in DB1 then don't anything
If the record doesn't exists in DB1 then add from DB2
If the record is in DB1 but not DB2 then update that record and set partition_key column to 1.
Any help will be appreciated. I am completely new to Oracle DBA.
If it's two users using:
MERGE INTO db1.student_lookup a
USING
(select * from db2.student_mater) b
ON (
a.id = b.id
AND <others join column>
)
WHEN MATCHED THEN UPDATE SET a.partition_key = 1
WHEN NOT MATCHED THEN INSERT (<a.column>)
VALUES (<b.column>)
If it's two db:
CREATE DATABASE LINK DBLINK_DB1_DB2
CONNECT TO DB2 IDENTIFIED BY <ENTER USER PASSWORD HERE>
USING '<FROM tnsnames>'
MERGE INTO db1.student_lookup a
USING
(select * from "student_mater"#"DBLINK_DB1_DB2") b
ON (
a.id = b.id
AND <others join column>
)
WHEN MATCHED THEN UPDATE SET a.partition_key = 1
WHEN NOT MATCHED THEN INSERT (<a.column>)
VALUES (<b.column>)
If you neen SP simple megre into your SP.
I am writing a script to pull out data from existing stored procedures. What I want to do is pull out all connections from a single source.
I.E. we have a select in a stored procedure. That select is as follows,
Select data from dbo.table1 t1
inner join dbo.table2 t2 on t1.pk=t2.pk
inner join dbo.table3 t3 on t2.pk=t3.pk
I want to pull out dbo.table1, dbo.table2, and dbo.table3
Edit:
To clear up, from that select statement, I want to pull out dbo.table1, dbo.table2, and dbo.table3 into an output or insert it into a table. Basically, I'm trying to get a list of all tables from all stored procedures.
Also, sysdepends does not work for every stored procedure due to some of the stored procedures existing at a linked server.
You can use sys.dm_sql_referenced_entities for SQL Server 2014 or sp_depends for previous versions.
Suppose I have two databases located on the same server. How can we write the query part in sql server to extract data from two different databases located on the same server.
Use 3 part naming: [DatabaseName].[SchemaName].[TableName]
select
t1.*,
t2.*
from [MyDatabase].[dbo].[MyTable] t1
join [MyOtherDatabase].[dbo].[MyOtherTable] t2 on t1.SomeColumn = t2.SomeColumn
Fully qualify the table with DB_NAME.Schema_name.table_name. For example, if you have database as DB1 and DB2 with default schema as dbo and table names tab1. then you can differentiate between them saying
select * from DB1.dbo.tab1
OR
select * from DB2.dbo.tab1
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.
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.