Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Stored procedure - update return value

Posted on 2009-05-14
2
Medium Priority
?
247 Views
Last Modified: 2012-05-07
I have a table (MyTable) with 3 columns (Id, A, B).
I want to update if column A has the same value as the parameter @A sent into the Stored Procedure.
How do i return the value of Id column after a row has been updated?

@A varchar(max)
@B varchar(max)
AS
if exists(SELECT * FROM MyTable WHERE A=@A)
begin
	update MyTable SET B=@B WHERE A=@A
END

Open in new window

0
Comment
Question by:johnkainn
2 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 1000 total points
ID: 24384591
try this
if Record exists it will return id otherwise 0

@A varchar(max)
@B varchar(max)
 
AS
Declare @ID int
Set @ID = 0
if exists(SELECT * FROM MyTable WHERE A=@A)
begin
	Select @id = ID ROM MyTable WHERE A=@A
 
	update MyTable SET B=@B WHERE A=@A
     
END
Return @ID

Open in new window

0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 1000 total points
ID: 24384679
you should use the OUTPUT option, that will return all the IDs in case there exists many IDs with the same @A

DECLARE @Out table (Id int )

update MyTable
SET B=@B
OUTPUT inserted.ID into @Out
WHERE A=@A

IF @@Rowcount > 0
  SELECT ID from @Out
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline

886 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