执行INSERT存储过程时“指定的参数太多”

时间:2013-10-03 07:40:53

标签: sql sql-server sql-server-2008 stored-procedures

我在MSSQL 2008 R2上遇到了存储过程。 它看起来是正确的,但是当我执行这个存储过程时,我收到错误“指定的参数太多”。 这是SP代码:

ALTER PROCEDURE [dbo].[InsertOrder] 
@KodT int, @kodG int, @Ad int, @Chd int, @PU datetime, @disc money, @Agent int, @PriceAd money, @PriceCh money 

AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @net money;
    DECLARE @netchd money;
    IF NOT EXISTS (SELECT КодЗ from Заказы Where Код = @KodT AND КодГ = @kodG)
    BEGIN
    SET @net = (SELECT Экскурсии.Net FROM Экскурсии INNER JOIN График ON График.Excursion = Экскурсии.Kod WHERE График.КодГ = @kodG);
    SET @netchd = (SELECT Экскурсии.NetChd FROM Экскурсии INNER JOIN График ON График.Excursion = Экскурсии.Kod WHERE График.КодГ = @kodG);
    END
    INSERT INTO Заказы (Код, КодГ, Pax, Child, PickUpTime, Discaunt, Продал, Price, PriceChd, Net, NetChd) 
    VALUES (@KodT, @KodG, @Ad, @Chd, @PU, @Disc, @Agent, @PriceAd, @PriceCh, @net, @netchd)
    END

这是vb.net代码:

With cmd
                    .Connection = ortsCon
                    .CommandText = "InsertOrder"
                    .CommandType = Data.CommandType.StoredProcedure
                    .Parameters.AddWithValue("@KodT", kodt)
                    .Parameters.AddWithValue("@KodG", kodg)
                    .Parameters.AddWithValue("@Ad", ad.Text)
                    .Parameters.AddWithValue("@Chd", ch.Text)
                    .Parameters.AddWithValue("@PU", pu.Text)
                    .Parameters.AddWithValue("@Disc", disc.Text)
                    .Parameters.AddWithValue("@Agent", userkod)
                    .Parameters.AddWithValue("@PriceAd", priceAd.Text)
                    .Parameters.AddWithValue("@PriceCh", priceCh.Text)
                End With
                ortsCon.Open()
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                ortsCon.Close()

1 个答案:

答案 0 :(得分:2)

如果您在vb代码的循环中执行此操作并且不在每次迭代之间清除命令对象(或删除参数),则每次都会向命令添加一组新参数。

这将导致第一次迭代正常运行,但第二次迭代将报告“指定的参数太多”。