我正在尝试编写一个存储过程,以一次将数据添加到三个表中。该存储过程将调用其他三个存储过程。我有每个参数的数据,但是在SQL Server中我不断收到丢失的参数错误。问题在于错误消息中给出的参数不是主存储过程的EXECUTE语句的一部分。运行sp_help
表示我正在将所需的所有参数传递给主过程。代码如下。
Error message: Msg 201, Level 16, State 4, Procedure uspAddCustomerJob, Line 0 [Batch Start Line 157]
Procedure or function 'uspAddCustomerJob' expects parameter '@intJobID', which was not supplied.
-- =========================================
-- stored procedure to add customer record to customer table
GO
CREATE PROCEDURE uspAddCustomer
@intCustomerID AS INTEGER = 0 OUTPUT
,@strName AS VARCHAR(250)
,@strPhone AS VARCHAR(250)
,@strEmail AS VARCHAR(250)
AS
SET XACT_ABORT ON
BEGIN TRANSACTION
INSERT INTO TCustomers(strName, strPhone, strEmail)
VALUES (@strname, @strPhone, @strEmail)
COMMIT TRANSACTION
GO
-- =================================================
-- stored procedure to add job to job table
GO
CREATE PROCEDURE uspAddJob
@intJobID AS INTEGER = 0 OUTPUT
,@strJobDescription AS VARCHAR(250)
,@dtmStartDate AS DATETIME
,@dtmEndDate AS DATETIME
AS
SET XACT_ABORT ON
BEGIN TRANSACTION
INSERT INTO TJobs(strJobDescription, dtmStartDate, dtmEndDate)
VALUES (@strJobDescription, @dtmStartDate, @dtmEndDate)
COMMIT TRANSACTION
GO
-- ===================================================
-- stored procedure to add PK's from previous tables to a third table (many-to-many relationship)
GO
CREATE PROCEDURE uspAddCustomerJob
@intCustomerJobID AS INTEGER OUTPUT
,@intCustomerID AS INTEGER
,@intJobID AS INTEGER
AS
SET XACT_ABORT ON
BEGIN TRANSACTION
INSERT INTO TCustomerJobs(intCustomerID, intJobID)
VALUES (@intCustomerID, @intJobID)
COMMIT TRANSACTION
GO
-- =====================================================
-- main stored procedure that calls each component stored procedure, with execution code at the bottom. Running the EXECUTE statement yields the error mentioned above.
GO
CREATE PROCEDURE uspAddCustomerAndJob --main procedure
@intCustomerJobID AS INTEGER = 0 OUTPUT
,@strName AS VARCHAR(250)
,@strPhone AS VARCHAR(250)
,@strEmail AS VARCHAR(250)
,@strJobDescription AS VARCHAR(250)
,@dtmStartDate AS DATETIME
,@dtmEndDate AS DATETIME
AS
SET XACT_ABORT ON
BEGIN TRANSACTION
DECLARE @intCustomerID AS INTEGER = 0
DECLARE @intJobID AS INTEGER = 0
EXECUTE uspAddCustomer @intCustomerID OUTPUT, @strName, @strPhone, @strEmail;
EXECUTE uspAddJob @intJobID OUTPUT, @strJobDescription, @dtmStartDate, @dtmEndDate;
EXECUTE uspAddCustomerJob @intCustomerJobID OUTPUT, @intJobID;
COMMIT TRANSACTION
GO
--TEST CODE
DECLARE @intCustomerJobID AS INTEGER;
EXECUTE uspAddCustomerAndJob @intCustomerJobID OUTPUT, 'Joe Smith', '513-555-9644', 'Jsmith@yahoo.com', 'Fix cracked sewer pipe', '6/1/2019', '6/5/2019'
答案 0 :(得分:0)
您执行的过程中发生错误。
“ uspAddCustomerJob”的过程定义具有3个参数:
ls
但它被称为2,缺少“ @intCustomerID”:
set
答案 1 :(得分:0)
该错误消息具有误导性。在uspAddCustomerAndJob
中,您缺少参数。
您的通话看起来像这样:
EXECUTE uspAddCustomerJob @intCustomerJobID OUTPUT, @intJobID;
SQL Server正在获取您的第一个整数参数并将其映射到子proc的第一个整数参数,因此它认为它正在获取@intCustomerID。
您可以通过在调用中显式命名参数来避免这种混乱,无论如何,为了某人(包括您自己)可能需要在一天的凌晨3:00 AM进行代码故障排除,您应该这样做。这样看起来像这样:
EXECUTE uspAddCustomerJob
@intCustomerJobID = @intCustomerJobID OUTPUT,
@intCustomerID = @intCustomerID
@intJobID = @intJobID;
当您的局部变量与参数命名相同时,这看起来很有趣,但是在适当的情况下,您可以灵活地以不同的方式命名它们。另外,没有任何遗漏的参数惊喜,或者至少可以更好地传达丢失的信息。