Friday, February 24, 2012

DatabaseName Problem

DECLARE @.DatabaseName varchar(10)
DECLARE @.OwnerName varchar(10)
DECLARE @.TableName varchar(10)

SET @.DatabaseName='TempDatabase'
SET @.OwnerName='XYZ'
SET @.TableName='TempTable'

select * from @.DatabaseName.@.OwnerName.@.TableName

this is not working.

SEND ME HELP AS SOON AS POSSIBLEDECLARE @.DatabaseName varchar(10)
DECLARE @.OwnerName varchar(10)
DECLARE @.TableName varchar(10)

SET @.DatabaseName='TempDatabase'
SET @.OwnerName='XYZ'
SET @.TableName='TempTable'

select * from @.DatabaseName.@.OwnerName.@.TableName

this is not working.

SEND ME HELP AS SOON AS POSSIBLE
you will have to use dynamic sql something like this:

DECLARE @.DatabaseName varchar(10)
DECLARE @.OwnerName varchar(10)
DECLARE @.TableName varchar(10)
declare @.sql nvarchar(1000)
SET @.DatabaseName='databasename'
SET @.OwnerName='dbo'
SET @.TableName='tablename'

set @.sql='select * from '+@.DatabaseName+'.'+@.OwnerName+'.'+@.TableName

exec @.sql|||It would also help to not assign 12 characters to a 10 character varchar().|||...but who's counting...