Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Results of a query to a variable
Message
De
22/05/2003 15:49:20
John Deupree
Long Term Care Authority
Tulsa, Oklahoma, États-Unis
 
 
À
22/05/2003 15:07:54
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
00791809
Message ID:
00791877
Vues:
16
Thanks.

>Try this:
>
>
>declare
>@sql nvarchar(128)
>,@var varchar(128)
>
>SET @sql = 'set @var = (select col from '+@table+' where pk = '+ @param + ')'
>exec sp_executesql @sql, N'@var varchar(128) output', @var output
>
>
>Of course, in the above you would have to cast @param if it were an integer. If it is a char, you will have to put quotes around it like:
>
>...+' where pk = '''+@param+''')'
>
>BOb
>
>
>
>
>>I have a stored proc that uses the following code:
>>
>>set @var = (select col from table where pk = @param)
>>
>>
>>What I would like to do is make 'table' a variable to make the sp more generic. Something like this would be nice (but this won't work).
>>
>>set @sql = 'select col from ' + @table + ' where pk = ' + @param
>>set @var = (exec sp_executesql @sql)
>>
>>
>>Any ideas how to set a variable to the result of a dynamically constructed sql statement?
>>
>>TIA
>>
>>John
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform