Solved

Setting a parameter value?

Posted on 2011-09-27
4
259 Views
Last Modified: 2012-05-12
How do I set a parameter to the result of an EXECUTE?
I tried this but got an error.
Set @Y  = EXECUTE (@X)

(the value of @X is a valid select statement)
So that :
Set @X = 'Select top(1) item from dbo.sales where ....'
EXECUTE (@X)
returns a value.
But I need the value assigned to @Y
Please help!
0
Comment
Question by:n2dweb
4 Comments
 
LVL 15

Accepted Solution

by:
Eyal earned 250 total points
ID: 36714427
0
 
LVL 9

Assisted Solution

by:mimran18
mimran18 earned 125 total points
ID: 36714560
Hi,
   Here is the sample.

Declare @X as nvarchar(100)
Declare @Item int

Set @X='Select top 1 @Item=[Item] from [Table Name]'
Exec sp_executesql @X,N'@Item int output',@Item output
Select @Item

0
 
LVL 9

Assisted Solution

by:sachinpatil10d
sachinpatil10d earned 125 total points
ID: 36714569
Try this
DECLARE @X nvarchar(500);
DECLARE @Y varchar(30);
SET @X = N'Select top(1) @out = item from dbo.sales';
exec sp_executesql @X, N'@out varchar(30) OUTPUT', @out=@Y OUTPUT;
SELECT @Y;

Open in new window

0
 
LVL 1

Author Closing Comment

by:n2dweb
ID: 36714676
Part of my issue was that I needed to use nVARCHAR and not VARCHAR
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now