SQL Server : create and call stored procedure

Exceli

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

Pரதீப்

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Server : create and call stored procedure

From Dev

Create stored procedure on SQL Server

From Dev

ASP parameters to call a stored procedure in SQL Server

From Dev

SQL server create stored procedure syntax error

From Dev

Create and execute stored procedure in SQL Server

From Dev

Create and execute stored procedure with parameter in SQL Server

From Dev

call a sql function stored in a separate db server from a stored procedure

From Dev

SQL Server - create stored procedure that runs several stored procedures sequentially

From Dev

Create Stored Procedure in SQL

From Dev

SQL server : stored procedure

From Dev

How to create a stored procedure on SQL server 2014 for create table operation?

From Dev

How to call sql server stored procedure with input parameters in Qt

From Dev

'Specified cast is not valid' error on call to SQL Server stored procedure

From Dev

Call SQL server stored procedure with JPA 2.1 annotations

From Dev

Call SQL Server stored procedure with parameters using C#

From Dev

Connection string for Access to call SQL Server stored procedure

From Dev

Call Stored Procedure with Null Data Parameter in SQL Server

From Dev

How to call sql server stored procedure with input parameters in Qt

From Dev

SQL Server: trying to create view inside a stored procedure

From Dev

In SQL Server, should I create synonym for a table or a stored procedure?

From Dev

Create text file from stored procedure SQL Server 2008

From Dev

How to create stored procedure in C#, then *save* it to SQL Server?

From Dev

SQL Server encryption : create key inside stored procedure

From Dev

How to create a stored procedure in SQL Server Management Studio

From Dev

Create stored procedure in SQL Server from a MS Access update query

From Dev

In SQL Server, should I create synonym for a table or a stored procedure?

From Dev

create a stored procedure that compare all the attribut betwen them with sql server

From Dev

How to create a select, and then an update stored procedure in SQL Server 2012

From Dev

How to create dynamic parameters in SQL Server stored procedure

Related Related

  1. 1

    SQL Server : create and call stored procedure

  2. 2

    Create stored procedure on SQL Server

  3. 3

    ASP parameters to call a stored procedure in SQL Server

  4. 4

    SQL server create stored procedure syntax error

  5. 5

    Create and execute stored procedure in SQL Server

  6. 6

    Create and execute stored procedure with parameter in SQL Server

  7. 7

    call a sql function stored in a separate db server from a stored procedure

  8. 8

    SQL Server - create stored procedure that runs several stored procedures sequentially

  9. 9

    Create Stored Procedure in SQL

  10. 10

    SQL server : stored procedure

  11. 11

    How to create a stored procedure on SQL server 2014 for create table operation?

  12. 12

    How to call sql server stored procedure with input parameters in Qt

  13. 13

    'Specified cast is not valid' error on call to SQL Server stored procedure

  14. 14

    Call SQL server stored procedure with JPA 2.1 annotations

  15. 15

    Call SQL Server stored procedure with parameters using C#

  16. 16

    Connection string for Access to call SQL Server stored procedure

  17. 17

    Call Stored Procedure with Null Data Parameter in SQL Server

  18. 18

    How to call sql server stored procedure with input parameters in Qt

  19. 19

    SQL Server: trying to create view inside a stored procedure

  20. 20

    In SQL Server, should I create synonym for a table or a stored procedure?

  21. 21

    Create text file from stored procedure SQL Server 2008

  22. 22

    How to create stored procedure in C#, then *save* it to SQL Server?

  23. 23

    SQL Server encryption : create key inside stored procedure

  24. 24

    How to create a stored procedure in SQL Server Management Studio

  25. 25

    Create stored procedure in SQL Server from a MS Access update query

  26. 26

    In SQL Server, should I create synonym for a table or a stored procedure?

  27. 27

    create a stored procedure that compare all the attribut betwen them with sql server

  28. 28

    How to create a select, and then an update stored procedure in SQL Server 2012

  29. 29

    How to create dynamic parameters in SQL Server stored procedure

HotTag

Archive