sql-server – 使用declare和set变量创建存储过程

前端之家收集整理的这篇文章主要介绍了sql-server – 使用declare和set变量创建存储过程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个只有值必须通过的存储过程,我必须从多个表中检索多个值.我试图这样做,但它的显示错误就像

Incorrect Syntax near Begin

Must declare the scalar variable @OrderID

Declare @OrderItemID AS INT
DECLARE @AppointmentID AS INT
DECLARE @PurchaSEOrderID AS INT
DECLARE @PurchaSEOrderItemID AS INT
DECLARE @SalesOrderID AS INT
DECLARE @SalesOrderItemID AS INT

SET @OrderItemID = (SELECT OrderItemID FROM [OrderItem] WHERE OrderID = @OrderID)

SET @AppointmentID = (SELECT  AppoinmentID FROM [Appointment] WHERE OrderID = @OrderID)

SET @PurchaSEOrderID = (SELECT  PurchaSEOrderID FROM [PurchaSEOrder] WHERE OrderID = @OrderID)

解决方法

您应该尝试此语法 – 假设您希望将@OrderID作为存储过程的参数:
CREATE PROCEDURE dbo.YourStoredProcNameHere
   @OrderID INT
AS
BEGIN
 DECLARE @OrderItemID AS INT
 DECLARE @AppointmentID AS INT
 DECLARE @PurchaSEOrderID AS INT
 DECLARE @PurchaSEOrderItemID AS INT
 DECLARE @SalesOrderID AS INT
 DECLARE @SalesOrderItemID AS INT

 SELECT @OrderItemID = OrderItemID 
 FROM [OrderItem] 
 WHERE OrderID = @OrderID

 SELECT @AppointmentID = AppoinmentID 
 FROM [Appointment] 
 WHERE OrderID = @OrderID

 SELECT @PurchaSEOrderID = PurchaSEOrderID 
 FROM [PurchaSEOrder] 
 WHERE OrderID = @OrderID

END

当然,只有当你返回一个值(不是多个值)时才有效

原文链接:https://www.f2er.com/mssql/75076.html

猜你在找的MsSQL相关文章