存储过程中的过程或函数指定了太多参数

时间:2019-03-11 19:03:01

标签: sql-server stored-procedures ado.net

我在使用以下代码遇到问题时,无法解决问题所在:

存储过程:

CREATE PROCEDURE [dbo].[spAddRateTypeRoomTypeCombination]
(
     @RatePlanName NVARCHAR(50),
     @OpenFrom DATE,
     @OpenTo DATE,
     @Active BIT,
     @RoomTypeId INT)
AS
    DECLARE @Result INT

   BEGIN TRANSACTION

   IF EXISTS (SELECT NULL FROM dbo.RateType WITH (UPDLOCK)
              WHERE RatePlanName = @RatePlanName) 
   BEGIN
       SELECT @Result = -1
   END
   ELSE
   BEGIN
       INSERT INTO dbo.RateType (RatePlanName, OpenFrom, OpenTo, Active)
       VALUES (@RatePlanName, @OpenFrom, @OpenTo, @Active)

        /******  return the last identity value inserted into an identity column ******/
       DECLARE @RateTypeid INT
       SET @RateTypeId = SCOPE_IDENTITY()

       INSERT INTO dbo.RateRoomCombination(RateTypeId, RoomTypeId)
       VALUES (@RateTypeId, @RoomTypeId)

       SELECT @Result = @@ERROR
   END

   IF @Result <> 0
   BEGIN
       ROLLBACK
   END
   ELSE
   BEGIN
       COMMIT
   END
   RETURN @Result
GO

这是我编写的用于将数据插入我的两个表RateTypeRateRoomCombination中的函数:

Function AddRateType(RatePlanName As String) As Integer
    Using con As SqlConnection = New SqlConnection(GetConnectionString())
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "spAddRateTypeRoomTypeCombination"
        cmd.Parameters.Add("@RatePlanName", SqlDbType.NVarChar).Value = TextBoxRateName.Text.Trim()
        cmd.Parameters.Add("@Active", SqlDbType.Bit).Value = CheckBox3.Checked
        cmd.Parameters.AddWithValue("@OpenFrom", Convert.ToDateTime(Textbox1.Text.Trim()))
        cmd.Parameters.AddWithValue("@OpenTo", Convert.ToDateTime(Textbox2.Text.Trim()))
        cmd.Parameters.Add(New SqlParameter("@Result", SqlDbType.Int))
        cmd.Parameters("@Result").Direction = ParameterDirection.ReturnValue
        For Each item As ListItem In chkRoomTypes.Items
            cmd.Parameters.Add(New SqlParameter("@RateTypeId", SqlDbType.Int))
            cmd.Parameters.Add(New SqlParameter("@RoomTypeId", SqlDbType.Int))
        Next
        Dim commandResult As Integer = 1
        cmd.Connection = con
        Try
            con.Open()
            cmd.ExecuteNonQuery()
            commandResult = CType(cmd.Parameters("@Result").Value, Integer)
        Catch ex As System.Data.SqlClient.SqlException
            commandResult = ex.Number
            ' Use the following 3 lines to understand better the error
            Dim msg As String = "Insert Error:"
            msg += ex.Message
            Throw New Exception(msg)
        Finally
            con.Close()
            con.Dispose()
        End Try
        Return commandResult
    End Using
End Function

我得到的错误:

  

System.Exception:'插入错误:过程或函数spAddRateTypeRoomTypeCombination指定的参数过多。'

如果我从SQL使用1 RateTypeId和1 RoomTypeId执行存储过程,则数据库将得到更新。如果我添加了1个以上的RoomTypeId(以尝试在页面上执行的情况为例),则SQL会引发与上述相同的错误:参数过多。

我真的不知道出什么问题了。

谢谢

0 个答案:

没有答案