SQL 服务器:创建并调用存储过程
SQL Server : create and call stored procedure
如何正确书写?
我正在编写一个存储过程,它将获取 (StockName, NewOpenPrice, NewClosePrice
),并将新记录添加到 table(如图所示),如果股票名称不存在。如果股票名称存在,则 OpenPrice 和 ClosePrice 将更新为新插入的价格。最后想调用存储过程
这是现在的样子
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)
我想这就是你需要的
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
或使用Merge
代替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)
执行
exec P_updatestock 'PFE',22.34,32.45
如何正确书写?
我正在编写一个存储过程,它将获取 (StockName, NewOpenPrice, NewClosePrice
),并将新记录添加到 table(如图所示),如果股票名称不存在。如果股票名称存在,则 OpenPrice 和 ClosePrice 将更新为新插入的价格。最后想调用存储过程
这是现在的样子
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)
我想这就是你需要的
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
或使用Merge
代替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)
执行
exec P_updatestock 'PFE',22.34,32.45