Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Storing variable name in variable and exec query

Author  Topic 

muralik
Starting Member

3 Posts

Posted - 2010-05-13 : 14:14:12
I try to run the following query

DECLARE @FN_SALU_NAME varchar(100)
DECLARE @TABLENAME varchar(1000)
DECLARE @FN_SALUTATION_ID varchar(5)
DECLARE @SQL varchar(5000)
SET @FN_SALUTATION_ID = 'AB'
SET @TABLENAME = 'PNET_MASTER_SALUTATION'

SELECT @SQL = 'SELECT @FN_SALU_NAME = SALUTATION
FROM ' + @TABLENAME +
' WHERE SALUTATIONID = ' + @FN_SALUTATION_ID

EXEC (@SQL)

Must declare the scalar variable "@FN_SALU_NAME".

But I have declared the above variable can you help me with this

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-05-13 : 15:11:20
You'll need to instead use sp_executesql if you need to set a variable inside the string.

Could you explain what you are trying to do?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -