SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


sp_executesql output parameter as table variable


sp_executesql output parameter as table variable

Author
Message
S_Kumar_S
S_Kumar_S
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1903 Visits: 1085
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

-----Table Proc Index Performance TSQL &&%$#@*(#@$%.......------------
Deep Into SQL Jungle
Ken McKelvey
Ken McKelvey
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3901 Visits: 8432
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search