473,836 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SELECT returning multiple values in a stored proc

Hi

I'm not sure what the best approach for this is:

I have a stored procedure which I would like to use to return several
output values instead of returning a recordset.

CREATE PROCEDURE Test (@param1 int, @param2 int OUTPUT, @param3 int
OUTPUT) AS
SELECT field2, field3 FROM Table WHERE field1 = @param1
I would like to return @param2 as field2 and @param3 as field3

How do I do this without using SELECT multiple times?

THanks in advance

Sam
Jul 20 '05 #1
6 42198


Samuel Hon wrote:
Hi

I'm not sure what the best approach for this is:

I have a stored procedure which I would like to use to return several
output values instead of returning a recordset.

CREATE PROCEDURE Test (@param1 int, @param2 int OUTPUT, @param3 int
OUTPUT) AS
SELECT field2, field3 FROM Table WHERE field1 = @param1

I would like to return @param2 as field2 and @param3 as field3

How do I do this without using SELECT multiple times?

THanks in advance

Sam


Did you try:

CREATE PROCEDURE Test (@param1 int, @param2 int OUTPUT, @param3 int OUTPUT)
AS SELECT @param2 = field2, @param3 = field3 FROM Table WHERE field1 = @param1

Joe
Jul 20 '05 #2
Samuel Hon (no*****@samuel hon.co.uk) writes:
I'm not sure what the best approach for this is:

I have a stored procedure which I would like to use to return several
output values instead of returning a recordset.

CREATE PROCEDURE Test (@param1 int, @param2 int OUTPUT, @param3 int
OUTPUT) AS
SELECT field2, field3 FROM Table WHERE field1 = @param1
I would like to return @param2 as field2 and @param3 as field3

How do I do this without using SELECT multiple times?


SELECT @param2 = field2, @param3 = field4 FROM tbl WHERE field = @param1

Note that this is only useful, if you know or can assume that the
SELECT returns only one row. If it returns more than one row, receiving
the data in output parameters is not a good idea. In that case you should
use a result set.
--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
Hi

I tried that and got this message

"A SELECT statement that assigns a value to a variable must not be
combined with data-retrieval operations"

I must be doing something stupid

Erland Sommarskog <so****@algonet .se> wrote in message news:<Xn******* **************@ 127.0.0.1>...
Samuel Hon (no*****@samuel hon.co.uk) writes:
I'm not sure what the best approach for this is:

I have a stored procedure which I would like to use to return several
output values instead of returning a recordset.

CREATE PROCEDURE Test (@param1 int, @param2 int OUTPUT, @param3 int
OUTPUT) AS
SELECT field2, field3 FROM Table WHERE field1 = @param1
I would like to return @param2 as field2 and @param3 as field3

How do I do this without using SELECT multiple times?


SELECT @param2 = field2, @param3 = field4 FROM tbl WHERE field = @param1

Note that this is only useful, if you know or can assume that the
SELECT returns only one row. If it returns more than one row, receiving
the data in output parameters is not a good idea. In that case you should
use a result set.

Jul 20 '05 #4
It seems that you have to assign all the values to parameters so I
cant do

SELECT @param2 = field2, field3 FROM tbl WHERE field = @param1

I have to do

SELECT @param2 = field2, @param3 = field3 FROM tbl WHERE field =
@param1

I dont need a recordset, just testing for existence of a record so the
where clause is a little more complex than I've shown

Thanks for the help

Erland Sommarskog <so****@algonet .se> wrote in message news:<Xn******* **************@ 127.0.0.1>...
Samuel Hon (no*****@samuel hon.co.uk) writes:
I'm not sure what the best approach for this is:

I have a stored procedure which I would like to use to return several
output values instead of returning a recordset.

CREATE PROCEDURE Test (@param1 int, @param2 int OUTPUT, @param3 int
OUTPUT) AS
SELECT field2, field3 FROM Table WHERE field1 = @param1
I would like to return @param2 as field2 and @param3 as field3

How do I do this without using SELECT multiple times?


SELECT @param2 = field2, @param3 = field4 FROM tbl WHERE field = @param1

Note that this is only useful, if you know or can assume that the
SELECT returns only one row. If it returns more than one row, receiving
the data in output parameters is not a good idea. In that case you should
use a result set.

Jul 20 '05 #5
Samuel Hon (no*****@samuel hon.co.uk) writes:
SELECT @param2 = field2, @param3 = field3 FROM tbl WHERE field =
@param1

I dont need a recordset, just testing for existence of a record so the
where clause is a little more complex than I've shown


In such case you should do:

CREATE PROCEDURE does_it_exist_s p @inputpar some_type,
@exists bit OUTPUT

SELECT @exists = CASE WHEN EXISTS (SELECT *
FROM tbl
WHERE col = @inputpar=
THEN 1
ELSE 0
END
--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #6
Thanks Erland

I'm returning a few fields about the exising record, hence the output params
CREATE PROCEDURE does_it_exist_s p @inputpar some_type,
@exists bit OUTPUT

SELECT @exists = CASE WHEN EXISTS (SELECT *
FROM tbl
WHERE col = @inputpar=
THEN 1
ELSE 0
END

Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8685
by: Stanley Sinclair | last post by:
I have a need to return multiple result sets from a stored procedure. Want that SP to call others to get the data. Win2003, db2 8.1.5. Can't figure out how to handle open cursors, and return >1 result sets. Thought about global temp tables.
6
26566
by: Terentius Neo | last post by:
Is it possible to combine (in DB2 UDB 8.1) a stored procedure and a select statement? I mean something like this: Select c.number, call procedure( c.number ) as list from table c With best regards
8
2094
by: CSDunn | last post by:
Hello, I have a situation in which I need to address three SQL Server 2000 Stored Procedure parameters in the OnClick event of an Option Group. The Option Group lives on an Access 2000 ADP form. In another situation where I had to set the RowSource of a combo box based on a parameter value that was delivered to a proc from another combo box, I did this: Private Sub FirstCombo_AfterUpdate()
1
1555
by: VM | last post by:
I'm working on a win appication that is constantly querying a small-sized DB. Until now, I've been using Selects from within my app but, all of a sudden I remembered of an application I was working on a year ago (with PowerBuilder) that queried the DB by invoking stored procedures that return the data to the application. One of the main problems was that there were over 100 SPs so it was pretty difficult to maintain. So my question: Is...
5
10969
by: Paul Aspinall | last post by:
Hi I have a Stored Proc in SQL server, which creates a record key when a record is created. I want to return the value back to my code, once the record has been created. I am using SQLHelper (Data Application Blocks for SQLServer), and calling the ExecuteNonQuery method in the SQLHelper class...
1
11586
by: Ahmet Karaca | last post by:
Hi. myds.Reset(); mycommand.SelectCommand.CommandText= "Select att1 from Ing as Ingredient, Pro as Product "+ "where Pro.ad='apple' and Pro.id=Ing.id"; mycommand.Fill(myds, "Product"); // Here is the problem listbox.DataSource = myds.Tables.DefaultView; // Here again listbox.DataTextField = "invid"; // Here again listbox.DataBind(); mycon.Close()
0
2236
by: rayone | last post by:
Hi folks. I need advice. 2 options, which do you think is the better option to display/retrieve/report on the data. Keep in mind reporting (Crystal), SQL Performance, VB Code, usability, architecture. Case 1: On a web page I would like to render a dropdown list
2
3032
by: jzogg7272 | last post by:
In my code I am executing a stored procedure to do a single row insert. I check the return value of the execution and I am getting -1, whereas a few weeks ago it was returning 0. Actually, I found that all of my insert stored procs are returning -1. The stored proc/insert statement is still executing successfully but the return code is different. If I execute the procedure from SQL Query Analyzer with the same params, it shows a return...
3
4393
by: codefragment | last post by:
Hi I have a chunky bit of sql that I will want to call from a number of places. It will return a few thousand rows. Whats the best way of structuring this? 1) I initially thought of using nested stored procedures and returning the result in a temporary table. However the scope of the temporary table seems to be limited to the stored procedure its created in so unless I create the temporary table in every stored
0
9810
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9656
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10575
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7774
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5812
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4001
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.