Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Stored procedure - update return value

Posted on 2009-05-14
2
219 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 250 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 250 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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