Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Database Stored Procedure

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am trying to write a stored procedure that has an inner join between two tables from two different databases on the same sql server.

Something like..

Select * FROM
DB1-table1 INNER JOIN DB2-table1
ON DB1-table1.ID = DB2-table1.ID

yada yada yada.......

Does anyone know how to do this or is it possible? If so, what database should I put the stored procedure in or does it matter?
 
Sure, you can do it. I would probably put the SP in the database where the bulk of the data resides, but no hard and fast rule.

[tt]Select *
FROM DB1..table1 TableA
INNER JOIN DB2..table1 TableB
ON TableA.ID = TableB.ID [/tt] Robert Bradley
teaser.jpg

 

You just need to fully qualify your table names as dbname.owner.tablename.

Select a.id, a.c1, a.c2, b.c3, b.c4
From db1.dbo.tbl1 a inner join db1.dbo.tbl2 b
On a.id=b.id

Make sure the login has permissions on both tables.

Good luck.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top