|
-
Jun 27th, 2005, 05:05 AM
#1
Thread Starter
Evil Genius
Table Name In A Variable
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!
-
Jun 27th, 2005, 07:05 AM
#2
Re: Table Name In A Variable
 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.
-
Jun 27th, 2005, 07:24 AM
#3
Thread Starter
Evil Genius
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|