简体   繁体   中英

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.

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. The exception is thrown when I call 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. You can declare them inside the SProc to make them "local".

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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