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

DB variable Name

Status
Not open for further replies.

VICKEL1

Programmer
May 16, 2005
15
GB
I have a 3 part DB naming convention such as

DB1.dbo.client

Is there any way to make the first part into a variable such as;

DECLARE @DB as varchar(20)
SET @DB = 'DB1'

SELECT * FROM @DB + '.dbo.client'

Any help would be appreciated
 
Only using dynamic SQL:

Code:
DECLARE @db varchar(20),
  @sql varchar(1000)

SET @db = 'mydb'
SET @sql = 'SELECT * FROM ' + @db + '.dbo.table'

EXEC(@sql)

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top