Unix Technical Forum

Insert SP results into variable?

This is a discussion on Insert SP results into variable? within the SQL Server forums, part of the Microsoft SQL Server category; --> Is it possible to take the result from an SP that returns 1 value and insert it into a ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:48 AM
jw56578@gmail.com
 
Posts: n/a
Default Insert SP results into variable?

Is it possible to take the result from an SP that returns 1 value and
insert it into a variable?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:49 AM
Madhivanan
 
Posts: n/a
Default Re: Insert SP results into variable?



DECLARE @x varchar(9), @sql1 nvarchar(200)
SELECT @sql1=N'select @x=count(*) from tt'
EXEC sp_executesql @sql1, N'@x varchar(9) OUTPUT', @x output
Select @x

Madhivanan

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:49 AM
Simon Hayes
 
Posts: n/a
Default Re: Insert SP results into variable?


<jw56578@gmail.com> wrote in message
news:1111709552.442972.201630@l41g2000cwc.googlegr oups.com...
> Is it possible to take the result from an SP that returns 1 value and
> insert it into a variable?
>


If a procedure returns one value, then you should use an output variable:

create proc dbo.p_out
@i int output
as
set @i = 1
go

declare @num int
exec dbo.p_out @i = @num output
select @num

This is usually the best approach for procedures which return only one row
of data:

http://www.sommarskog.se/share_data.html#OUTPUT

Simon


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:02 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com