Suppose I have a table variable defined as:
DECLARE @FilteredIDs TABLE(ID UNIQUEIDENTIFIER, UNIQUE CLUSTERED (ID))
Functions can return table variables, so supposed I have a function that accepts some input and returns @OutputTable
(with identical definition). Now suppose that I want to call that function multiple times in a loop passing it different parameters each time, and each time I want to intersect @FilteredIDs
with that @OutputTable
and assign the resulting intersected set back to the @FilteredIDs
variable, so that when the loop finishes, @FilteredIDs
will contain the result of all the intersections of all iterations.
I cannot find any information on how to set the value of a table variable. Once declared, I can only use it like a table, not a table 'variable' (e.g. set @T1 = @T2
does not appear to be a valid statement). I could truncate the table and reinsert data, but since I've already inserted the data into a 3rd table variable, I would just like the first table variable to refer to the 3rd table, without having to truncate the first table and reinsert all the data.
If this is not possible, then might there be a way to rename temp tables in this fashion, so that when the same temp table name is used later in the procedure, it refers to a different table than it did at the start of the procedure?