January 18, 2011 at 10:44 am
Hi All
I was just thinking of using a table variable instead of temp table to see if it gives any performance gain(no of records is less and I dont need any indexing on temp table) in a dynamic sql.
This is what I was trying to achieve:
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)
DECLARE @tabvarO table(id int)
DECLARE @Lastlname varchar(30)
SET @SQLString = N'DECLARE @tabvar table(id int)
insert into @tabvar SELECT account_card_id
FROM t_account_card WHERE last_name = @Lastlname'
SET @ParmDefinition = N'@Lastlname varchar(30),@tabvar table(id int) OUTPUT'
EXECUTE sp_executesql
@SQLString,
@ParmDefinition,
@Lastlname = 'smith',
@tabvar=@tabvarO OUTPUT
select * from @tabvarO
but I cant get the output in table variable and get error:
Msg 137, Level 15, State 2, Line 14
Must declare the scalar variable "@tabvarO".
I also understand that scope of variable for dynamic sql is for that statement only and that cannot be used outside the statement. But I'll appreciate any other inputs that might make it possible to get table variable out using dynamic sql.
thanks
Online Trainer For SQL DBA and Developer @RedBushTechnologies with 18 yrs exp.
January 19, 2011 at 3:52 am
To pass a table variable you have to create a type and define the table as the type.
What you are trying to do will not work as table variables can only be passed as READONLY.
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply