Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

If Exists return Identity else Insert...

Posted on 2006-05-21
3
Medium Priority
?
645 Views
Last Modified: 2012-08-14
Hi folks...

I'm looking to check to see if a price exists and if it does, get the PriceId - otherwise I want to insert the new price and return its PriceId... I've got the following stored procedure below and I've put in a comment where I have the problem. The insert is fine - it's getting at the PriceId if one already exists that I'm stuck on...

Cheers!

CREATE PROCEDURE sp_Price_CreatePrice
@PriceAmount money,
@PriceId int OUTPUT
AS

if EXISTS(
      SELECT PriceId FROM Price
      WHERE PriceAmount = @PriceAmount )
begin
      
/* I want to set @PriceId to be the PriceId found in the above if exists query */

end else begin

      INSERT INTO Price(PriceAmount) VALUES(@PriceAmount)

      SET @PriceId = @@IDENTITY
end

GO
0
Comment
Question by:zero303
  • 2
3 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16728223
CREATE PROCEDURE sp_Price_CreatePrice
@PriceAmount money,
@PriceId int OUTPUT
AS

if EXISTS(
     SELECT PriceId FROM Price
     WHERE PriceAmount = @PriceAmount )
begin
     
/* I want to set @PriceId to be the PriceId found in the above if exists query */

 SET  PriceID = PriceId FROM Price
 WHERE PriceAmount = @PriceAmount


end else begin

     INSERT INTO Price(PriceAmount) VALUES(@PriceAmount)

     SET @PriceId =SCOPE_IDENTITY()   --------Replaced @@Identity with SCOPE_IDENTITY()
end

GO
0
 
LVL 3

Author Comment

by:zero303
ID: 16728261
I'm getting Error near keyword FROM as shown below - I've sourced it this line I think...

CREATE PROCEDURE sp_Price_CreatePrice
@PriceAmount money,
@PriceId int OUTPUT
AS

if EXISTS(
      SELECT PriceId FROM Price
      WHERE PriceAmount = @PriceAmount )
begin
      SET  @PriceID = PriceId FROM Price          ------- I'm getting Error near keyword FROM
       WHERE PriceAmount = @PriceAmount
end else begin

      INSERT INTO Price(PriceAmount) VALUES(@PriceAmount)

      SET @PriceId = SCOPE_IDENTITY()
end
GO
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 16728264
CREATE PROCEDURE sp_Price_CreatePrice
@PriceAmount money,
@PriceId int OUTPUT
AS

if EXISTS(
     SELECT PriceId FROM Price
     WHERE PriceAmount = @PriceAmount )
begin
     SELECT  @PriceID = PriceId FROM Price          -------Change set to SELECT
      WHERE PriceAmount = @PriceAmount
end else begin

     INSERT INTO Price(PriceAmount) VALUES(@PriceAmount)

     SET @PriceId = SCOPE_IDENTITY()
end
GO
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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

577 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