简体   繁体   English

在存储过程中初始化参数

[英]Initializing parameters in a stored procedure

I am getting an exception when executing a stored procedure: 执行存储过程时出现异常:

Procedure or function 'spAddItemByUrl' expects parameter '@userId', which was not supplied. 过程或函数'spAddItemByUrl'需要未提供的参数'@userId'。

But I have initialized it, so what I'm dong wrong? 但是我已经初始化了,所以我错了吗?

Here is the stored procedure: 这是存储过程:

create proc spAddItemByUrl
    @title nvarchar(50),
    @body nvarchar(50),
    @link nvarchar(50),
    @userName nvarchar(50),
    @url nvarchar(50),
    @userId int,
    @feedId int
as
begin
    insert into feed(title, body, link) values(@title, @body, @link)
    select @userId = id from users where name = @userName
    select @feedId = id from feed where title = @title
    insert into userstofeed(userid, feedid) values(@userId, @feedId) 
    insert into feedurl(url, feedid, userid) values(@url, @feedId, @userId)
end

And here's the C# code. 这是C#代码。 The exception is thrown when I call ExecuteNonQuery : 当我调用ExecuteNonQuery时抛出异常:

connection.SqlCommand.Parameters.AddWithValue("@url", urlFeed.Url);
connection.SqlCommand.Parameters.AddWithValue("@title", item.Title.Text);
connection.SqlCommand.Parameters.AddWithValue("@body", item.Summary.Text);
connection.SqlCommand.Parameters.AddWithValue("@link", item.Id);
connection.SqlCommand.Parameters.AddWithValue("@userName", user);
connection.SqlCommand.ExecuteNonQuery();
connection.SqlCommand.Parameters.Clear();

You've added @userId int, @feedId int to the list of parameters for your SProc, but from the query it looks like you don't want them to be provided by the user. 你已经添加@userId int, @feedId int来为您的存储过程的参数列表,但是从查询它看起来像你不希望他们被用户提供做。 You can declare them inside the SProc to make them "local". 您可以在SProc中声明它们以使其成为“本地”。

ie

create proc spAddItemByUrl
    @title nvarchar(50),
    @body nvarchar(50),
    @link nvarchar(50),
    @userName nvarchar(50),
    @url nvarchar(50)
as
begin
    DECLARE @userId int, @feedId int;

    insert into feed(title, body, link) values(@title, @body, @link)
    select @userId = id from users where name = @userName
    select @feedId = id from feed where title = @title
    insert into userstofeed(userid, feedid) values(@userId, @feedId) 
    insert into feedurl(url, feedid, userid) values(@url, @feedId, @userId)
end

You've specified 您已指定

connection.SqlCommand.Parameters.AddWithValue("@userName", user);

but not @userid 但不是@userid

If you want to add default values to your stored procedure arguments you can do the following: 如果要将默认值添加到存储过程参数,则可以执行以下操作:

create proc spAddItemByUrl
...
@userId int = 0, /* or whatever you want the default value to be */
@feedId int = 0

If you decide to call your stored procedure, you then only have to pass the five arguments provided in your code, and the values @userId and @feedId will be 0. If you decide to specify values for those two parameters in your calling function, then the values you pass will be used. 如果决定调用存储过程,则只需传递代码中提供的五个参数, @userId@feedId的值将为0。如果决定在调用函数中为这两个参数指定值,那么将使用您传递的值。

Since your stored procedure doesn't use those parameters though, you should remove them from the section they are in. 由于您的存储过程虽然未使用这些参数,所以应从它们所在的部分中将其删除。

Instead just do 而是做

declare @userId int = select id from users where name = @userName
declare @feedId int = select id from feed where title = @title

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM