Hello,
Can someone please help me with this problem? Below is my sample code:
DECLARE @dynVar1 varchar(2)
DECLARE @dynVar2 varchar(2)
DECLARE @dynVar3 varchar(2)
DECLARE @runSQL varchar(500)
DECLARE @myVariable varchar(50)
SET @runSQL = ('SELECT @dynVar1=SomeField2, @dynVar2=SomeField2, @dynVar3=SomeField3
FROM MyDB.dbo.' + @dynTBL + ' WHERE myField = ''' + @myCriteria + '''')
EXEC(@runSQL)
IF @dynVar1 = '' --IS NULL
SET @myVariable = 'something'
ELSE
BEGIN
IF @dynVar2 = '' --IS NULL
SET @myVariable = 'something_else1'
ELSE
BEGIN
IF @dynVar3 = '' --IS NULL
SET @myVariable = 'something_else2'
ELSE
BEGIN
IF @dynVar3 = '01'
SET @myVariable = 'something_else3'
END
END
END
When I tried to run this from Query Analyzer (MS SQL 2000), I'd get error:
"Must declare the variable '@dynVar1'."
(The error must be at line "IF @dynVar1 = '' --IS NULL", I think)
Can someone please point out what my errors are in the code? I have to use "SET @runSQL ..." and "EXEC(@runSQL)" because the table to call from is dynamic and will be defined at run time.
Any help will be appreciated.
Thanks.
Allan
Can someone please help me with this problem? Below is my sample code:
DECLARE @dynVar1 varchar(2)
DECLARE @dynVar2 varchar(2)
DECLARE @dynVar3 varchar(2)
DECLARE @runSQL varchar(500)
DECLARE @myVariable varchar(50)
SET @runSQL = ('SELECT @dynVar1=SomeField2, @dynVar2=SomeField2, @dynVar3=SomeField3
FROM MyDB.dbo.' + @dynTBL + ' WHERE myField = ''' + @myCriteria + '''')
EXEC(@runSQL)
IF @dynVar1 = '' --IS NULL
SET @myVariable = 'something'
ELSE
BEGIN
IF @dynVar2 = '' --IS NULL
SET @myVariable = 'something_else1'
ELSE
BEGIN
IF @dynVar3 = '' --IS NULL
SET @myVariable = 'something_else2'
ELSE
BEGIN
IF @dynVar3 = '01'
SET @myVariable = 'something_else3'
END
END
END
When I tried to run this from Query Analyzer (MS SQL 2000), I'd get error:
"Must declare the variable '@dynVar1'."
(The error must be at line "IF @dynVar1 = '' --IS NULL", I think)
Can someone please point out what my errors are in the code? I have to use "SET @runSQL ..." and "EXEC(@runSQL)" because the table to call from is dynamic and will be defined at run time.
Any help will be appreciated.
Thanks.
Allan