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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 problem with variables

Author  Topic 

nsrao_1975
Starting Member

13 Posts

Posted - 2004-02-26 : 01:50:16

hi,

can any guru help me out here..

set @var1='inst1' -- value i will be knowing only in runtime
set @var1='select '+@var1+' from table_1 where <cond>'
here i want to set the output value of the @var1 to @var2

i can't give with case condition becos inst[n] where n can be upto any value..

thanks inavance

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-02-26 : 03:50:33
Perhaps if you create a temp table first.
Insert the select statement from @var1 into the temp table

Set @var2 = SELECT x from #Temp)

Something like this:

--*************************************************

DECLARE @Var1 VarChar(4000)
DECLARE @Var2 INT

CREATE TABLE #Temp(BranchCode INT)

SET @Var1 = 'INSERT INTO #Temp SELECT TOP 1 BranchCode FROM Branch'

EXEC(@Var1)
SET @Var2 = (SELECT BranchCode FROM #Temp)

SELECT @Var2



Duane.

OOPS! Didnt realise that the same topic was posted twice.
Go to Top of Page

sachinsamuel
Constraint Violating Yak Guru

383 Posts

Posted - 2004-02-26 : 04:37:01
U can even go for sp_executesql or exec to execute the dynamic sql u are creating. See bol for more information
Go to Top of Page
   

- Advertisement -