程序或功能' System_Set_Cookies'期望参数' @ name',这是未提供的

时间:2017-10-31 09:26:49

标签: sql sql-server stored-procedures

我在执行存储过程时遇到此错误:

程序或功能' System_Set_Cookies'期望参数' @ name',这是未提供的。

但是你可以看到我已经声明了@name,它的值也是整数。

**-- Executing my SP**
DECLARE @name int, @Id int, @value nvarchar(255), @update bit;
SET @name = 0;
SET @Id = 0;
SET @value = 'Development';
SET @update = 0;
EXEC System_Set_Cookies;    

**-- Stored Procedure Query**
ALTER PROCEDURE [dbo].[System_Set_Cookies]
            -- Add the parameters for the stored procedure here
            @name INT,
            @Id INT,
            @value NVARCHAR(255),
            @update BIT
        AS
        BEGIN
            -- SET NOCOUNT ON added to prevent extra result sets from
            -- interfering with SELECT statements.
            SET NOCOUNT ON;

            -- Insert statements for procedure here
            IF @update = 1
                BEGIN
                    SELECT @Id = COOKIE_ID FROM tbl_Cookies WHERE cookie_name = @name;

                    UPDATE tbl_Cookies SET cookie_name = @name WHERE COOKIE_ID = @Id;

                    UPDATE tbl_Cookies_User SET cookie_value = @value WHERE cookie_id = @Id;
                END
            ELSE
                BEGIN
                    INSERT INTO tbl_Cookies (cookie_name) VALUES (@name);

                    SELECT @Id = MAX(COOKIE_ID) FROM tbl_Cookies;

                    INSERT INTO tbl_Cookies_User (cookie_id, cookie_value) VALUES (@Id, @value);
                END
        END

1 个答案:

答案 0 :(得分:1)

仅仅因为您的变量名称与存储过程的参数名称匹配,SQL Server并不假设您必须将该变量作为参数传递。你必须明确:

DECLARE @name int, @Id int, @value nvarchar(255), @update bit;
SET @name = 0;
SET @Id = 0;
SET @value = 'Development';
SET @update = 0;
EXEC System_Set_Cookies @name, @id, @value, @update;

这些名字甚至不必匹配:

DECLARE @OOname int, @Id int, @value nvarchar(255), @update bit;
SET @OOname = 0;
SET @Id = 0;
SET @value = 'Development';
SET @update = 0;
EXEC System_Set_Cookies @name = @OOname, @id, @value, @update;

(从C#调用时,请确保将CommandText设置为存储过程的名称,将CommandType设置为StoredProcedure,然后自动匹配参数而不必在那里明确)