Click to See Complete Forum and Search --> : Not a temp table, but a temp table variable?


filthy_mcnasty
June 15th, 2009, 06:50 PM
Hi

Basically I want to write a stored procedure that will update data across 2+ different tables but the data will be in one or the other, not both. Please assume I can determine WHICH table the data I'm looking for is in....

I'm hoping to do something like:

declare @temptbl table

if(@row_count > 0)
begin
set @temptbl = db.schema.tablename
end
else
begin
set @temptbl = db.schema.backupdata
end

update @temptbl
set bla = 'bla'



Basically I'd like to avoid having 4000 nests of boolean logic here. Is there an easy way to do something like this WITHOUT modifying the data structure? (i cannot create a view w/ partition)

hspc
June 16th, 2009, 05:18 PM
Temp tables in T-SQL don't work the same way as references in programming languages, so the code you sent will not work.

I think you'll need to construct a dynamic SQL statement and use sp_executesql (http://msdn.microsoft.com/en-us/library/ms175170.aspx).