Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Select from 2 databases

Author  Topic 

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2007-01-29 : 14:13:16
I have a stored procedure that I need to get working and have searched with no luck on how to do this.

The sp is in one database and it does a simple select from a table.

I need to do a union on another table that is in a seperate database but the same server....


Select * from myTable Where myField = 1
UNION
Select * from myOtherDatabase.myOtherTable Where myOtherField = 1


How can I access the other database?

Thanks,

Zath

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2007-01-29 : 14:16:12
Never fails.... I found it.

databaseName.dbo.tableName


Zath
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2007-01-29 : 14:17:34

Select * from myTable Where myField = 1
UNION
Select * from myOtherDatabase..myOtherTable Where myOtherField = 1

You will need permission on the other table for the the user that runs the sp.
The syntax is

db.owner.table
Try it first with no owner as I have in your statement - that will default to dbo or the user.



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -