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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a defined variable to define the database?

Status
Not open for further replies.

NSMan

Technical User
Aug 26, 2004
72
US
How can I do this?

declare @database varchar(20)
set @database='img000029'
select * from @database.dbo.imgs
 
You can use dynamic sql to do this. Dynamic SQL is best avoided if possible.

Code:
declare @database varchar(20)
Declare @SQL VarChar(8000)
set @database='img000029'

Set @SQL = 'select * from [' + @database + '].dbo.imgs'
Exec (@SQL)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top