Solved

Setting a parameter value?

Posted on 2011-09-27
4
263 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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