Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Stored procedure - update return value

Posted on 2009-05-14
2
Medium Priority
?
237 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
[X]
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
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

721 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