1

How do I write this correctly?

I am writing a stored procedure that will take (StockName, NewOpenPrice, NewClosePrice), and add a new record to the table (shown in image), IF the stockname does not EXIST. IF the stockname EXISTs, then OpenPrice and ClosePrice will be updated with the newly inserted prices. Finally I want to call the stored procedure

This is what it looks like now

CREATE PROCEDURE p_updatestock
    (
    @StockName VARCHAR(50), 
    @OpenPrice MONEY,
    @ClosePrice MONEY)
AS
    Declare @NewOpenPrice MONEY
    Declare @NewClosePrice MONEY

    UPDATE Stocks 
    SET StockName = @StockName
    SET @StockName = @rowcount

    UPDATE Stocks 
    SET NewOpenPrice = @NewOpenPrice
    WHERE OpenPrice = @NewOpenPrice 
    SET @NewOpenPrice = @rowcount

    IF (@StockName EXIST THEN OpenPrice)
        UPDATE Stocks 
        SET NewClosePrice = @NewClosePrice
        WHERE ClosePrice = @NewClosePrice
        SET @NewClosePrice = @rowcount

        IF (@StockName EXIST THEN ClosePrice)

enter image description here

1 Answer 1

2

I think this is what you need

CREATE PROCEDURE P_updatestock (@StockName  VARCHAR(50),
                                @OpenPrice  MONEY,
                                @ClosePrice MONEY)
AS
  BEGIN
      IF EXISTS (SELECT 1
                 FROM   Stocks
                 WHERE  StockName = @StockName)
        UPDATE Stocks
        SET    ClosePrice = @ClosePrice,
               Openprice = @Openprice
        WHERE  StockName = @StockName
      ELSE
        INSERT INTO stocks
                    (StockName,
                     Openprice,
                     ClosePrice)
        VALUES      (@StockName,
                     @Openprice,
                     @ClosePrice)
  END 

Or use Merge instead of If-Else

MERGE Stocks AS target
USING (SELECT @StockName,
              @OpenPrice,
              @ClosePrice) AS source (StockName, OpenPrice, ClosePrice)
ON ( target.StockName = source.StockName )
WHEN MATCHED THEN
  UPDATE SET ClosePrice = source.ClosePrice,
             Openprice = source.Openprice
WHEN NOT MATCHED THEN
  INSERT (StockName,
          Openprice,
          ClosePrice)
  VALUES (source.StockName,
          source.Openprice,
          source.closeprice) 

To execute

  exec P_updatestock 'PFE',22.34,32.45
Sign up to request clarification or add additional context in comments.

3 Comments

WOW! THANKS NoDisplayName. Let me run it and see. How do I call it?
Got this error messageMsg 137, Level 15, State 2, Procedure P_updatespock, Line 10 Must declare the scalar variable "@NewClosePrice". Msg 137, Level 15, State 2, Procedure P_updatespock, Line 19 Must declare the scalar variable "@NewOpenprice".
Alright. It worked. Thank you once again. I was thinking "Declare Openstock Money" and "Closestok Money" is what it needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.