Re: Table Name In A Variable
Quote:
Originally Posted by alex_read
Is it possible to declare and use a table name please. I am trying to get this type of query to run, but cannot find the right syntax:
Code:
DECLARE @TableName nvarchar
IF @MyPassedInBitVariable = 0
SET @TableName = [MyDB..MyTableName1]
ELSE
SET @TableName = [MyDB..MyOtherTableName2]
SELECT @TableName.Column1
FROM @TableName
Thanks!
No, not the way you do it here. One way to solve the problem is to assign the table name to the @TableName variable and then build a TSQL statement into another variable that you execute with sp_executesql. You could also execute the SELECT statements directly in the IF...ELSE code.
Re: Table Name In A Variable
Ok, yeah I found examples of those thanks, was just wondering if I could do it in that way. Oh well, thanks for clearing that up for me, cheers! :D