gpt4 book ai didi

sql - 过程或功能!指定的参数太多

转载 作者:太空狗 更新时间:2023-10-30 01:38:40 25 4
gpt4 key购买 nike

我正在 SQL Server 2008 中开发我的第一个存储过程,需要有关错误消息的建议。

Procedure or function xxx too many arguments specified

这是我在执行存储过程 [dbo].[M_UPDATES] 后得到的,它调用另一个名为 etl_M_Update_Promo 的存储过程。

当通过鼠标右键单击和“执行存储过程”调用 [dbo].[M_UPDATES](代码见下文)时,查询窗口中出现的查询是:

USE [Database_Test]
GO

DECLARE @return_value int

EXEC @return_value = [dbo].[M_UPDATES]

SELECT 'Return Value' = @return_value

GO

输出是

Msg 8144, Level 16, State 2, Procedure etl_M_Update_Promo, Line 0
Procedure or function etl_M_Update_Promo has too many arguments specified.

问题:此错误消息的确切含义是什么,即哪里有太多参数?如何识别它们?

我发现有几个线程询问此错误消息,但提供的代码与我的代码完全不同(如果不是使用另一种语言,如 C#)。所以没有一个答案解决了我的 SQL 查询(即 SP)的问题。

注意:下面我提供了两个SP使用的代码,但是我更改了数据库名、表名和列名。所以,请不要担心命名约定,这些只是示例名称!

(1) SP1 代码 [dbo].[M_UPDATES]

USE [Database_Test]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[ M_UPDATES] AS
declare @GenID bigint
declare @Description nvarchar(50)

Set @GenID = SCOPE_IDENTITY()
Set @Description = 'M Update'

BEGIN
EXEC etl.etl_M_Update_Promo @GenID, @Description
END

GO

(2) SP2 [etl_M_Update_Promo] 代码

USE [Database_Test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [etl].[etl_M_Update_Promo]
@GenId bigint = 0
as

declare @start datetime = getdate ()
declare @Process varchar (100) = 'Update_Promo'
declare @SummeryOfTable TABLE (Change varchar (20))
declare @Description nvarchar(50)
declare @ErrorNo int
, @ErrorMsg varchar (max)
declare @Inserts int = 0
, @Updates int = 0
, @Deleted int = 0
, @OwnGenId bit = 0

begin try


if @GenId = 0 begin
INSERT INTO Logging.dbo.ETL_Gen (Starttime)
VALUES (@start)

SET @GenId = SCOPE_IDENTITY()
SET @OwnGenId = 1
end


MERGE [Database_Test].[dbo].[Promo] AS TARGET
USING OPENQUERY( M ,'select * from m.PROMO' ) AS SOURCE
ON (TARGET.[E] = SOURCE.[E])


WHEN MATCHED AND TARGET.[A] <> SOURCE.[A]
OR TARGET.[B] <> SOURCE.[B]
OR TARGET.[C] <> SOURCE.[C]
THEN
UPDATE SET TARGET.[A] = SOURCE.[A]
,TARGET.[B] = SOURCE.[B]
, TARGET.[C] = SOURCE.[c]

WHEN NOT MATCHED BY TARGET THEN
INSERT ([E]
,[A]
,[B]
,[C]
,[D]
,[F]
,[G]
,[H]
,[I]
,[J]
,[K]
,[L]
)
VALUES (SOURCE.[E]
,SOURCE.[A]
,SOURCE.[B]
,SOURCE.[C]
,SOURCE.[D]
,SOURCE.[F]
,SOURCE.[G]
,SOURCE.[H]
,SOURCE.[I]
,SOURCE.[J]
,SOURCE.[K]
,SOURCE.[L]
)

OUTPUT $ACTION INTO @SummeryOfTable;


with cte as (
SELECT
Change,
COUNT(*) AS CountPerChange
FROM @SummeryOfTable
GROUP BY Change
)

SELECT
@Inserts =
CASE Change
WHEN 'INSERT' THEN CountPerChange ELSE @Inserts
END,
@Updates =
CASE Change
WHEN 'UPDATE' THEN CountPerChange ELSE @Updates
END,
@Deleted =
CASE Change
WHEN 'DELETE' THEN CountPerChange ELSE @Deleted
END
FROM cte


INSERT INTO Logging.dbo.ETL_log (GenID, Startdate, Enddate, Process, Message, Inserts, Updates, Deleted,Description)
VALUES (@GenId, @start, GETDATE(), @Process, 'ETL succeded', @Inserts, @Updates, @Deleted,@Description)


if @OwnGenId = 1
UPDATE Logging.dbo.ETL_Gen
SET Endtime = GETDATE()
WHERE ID = @GenId

end try
begin catch

SET @ErrorNo = ERROR_NUMBER()
SET @ErrorMsg = ERROR_MESSAGE()

INSERT INTO Logging.dbo.ETL_Log (GenId, Startdate, Enddate, Process, Message, ErrorNo, Description)
VALUES (@GenId, @start, GETDATE(), @Process, @ErrorMsg, @ErrorNo,@Description)


end catch
GO

最佳答案

您使用 2 个参数(@GenId 和 @Description)调用该函数:

EXEC etl.etl_M_Update_Promo @GenID, @Description

但是你已经声明函数接受 1 个参数:

ALTER PROCEDURE [etl].[etl_M_Update_Promo]
@GenId bigint = 0

SQL Server 告诉你 [etl_M_Update_Promo] 只需要 1 个参数 (@GenId)

您可以通过指定 @Description 来更改过程以采用两个参数。

ALTER PROCEDURE [etl].[etl_M_Update_Promo]
@GenId bigint = 0,
@Description NVARCHAR(50)
AS

.... Rest of your code.

关于sql - 过程或功能!指定的参数太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17292705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com