在存储过程sql 2008中声明变量

时间:2014-06-18 07:46:05

标签: sql vb.net sql-server-2008 stored-procedures declare

我正在开发一个使用存储过程的项目。当我尝试在项目中使用存储过程时,我收到此错误:

The formal parameter "@Mode" was not declared as an OUTPUT parameter, but the actual parameter passed in requested output.

请帮我解决这个问题。

这是我的存储过程:

alter PROCEDURE [ITAssets_sp_IT_Assets]
-- Add the parameters for the stored procedure here

    (@Mode varchar(12)='ADD',
    @ID integer , @AssetCode nvarchar(20)=null, @Description nvarchar(70)=null,
    @Site nvarchar(10)=null)
AS
Begin

    IF @Mode='ADD'
        Begin
  Begin Tran
      INSERT INTO [IT_Assets]
          ([ID]
                ,[AssetCode]
                ,[Description]
                ,[Site])
                    values
    (@ID, @AssetCode, @Description, @Site
    )
     If @@ERROR <> 0  
            ROLLBACK TRAN 
        Else
            COMMIT TRAN

    Select  @ID
End
ELSE 
Begin
        Begin Tran
                UPDATE [IT_Assets]
                    SET 
    AssetCode = @AssetCode, Description = @Description, Site = @Site 
    WHERE ID = @ID      
    If @@ERROR <> 0  
            ROLLBACK TRAN 
        Else
            COMMIT TRAN
        Select  @ID 
End
End

我在下面的代码中调用存储过程:

Dim ht As New Hashtable

ht.Add("@Mode", "ADD")
ht.Add("@ID", txtID.Text)
ht.Add("@AssetCode", txtAssetCode.Text)
ht.Add("@Description", txtDescription)
ht.Add("@Site", ddlSite.SelectedValue.ToString())

AppExecuteNonQuery(CommandType.StoredProcedure, "IT_Assets", , ht)

AppExecuteNonQuery代码:

 Public Shared Function AppExecuteNonQuery(ByVal SQLCommandType As CommandType, ByVal Command As String, Optional ByVal ParamTable As Hashtable = Nothing, Optional ByRef OutputParamTable As Hashtable = Nothing, Optional ByVal UsePrimaryConnection As Boolean = True, Optional ByVal ConnName As String = "", Optional ByVal ConnString As String = "") As Integer
        Dim cmd As SqlCommand
        Try
            If UsePrimaryConnection Then
                cmd = AppGetSQLCommand(SQLCommandType, Command, ParamTable, OutputParamTable, AppProperties.ConnectionString, True, UsePrimaryConnection, ConnName, ConnString)
            Else
                cmd = AppGetSQLCommand(SQLCommandType, Command, ParamTable, OutputParamTable, AppProperties.ConnectionString1, True, UsePrimaryConnection, ConnName, ConnString)
            End If

            Dim res As Integer = cmd.ExecuteNonQuery

            For Each param As SqlParameter In cmd.Parameters
                If param.Direction = ParameterDirection.Output Then
                    OutputParamTable(param.ParameterName) = param.Value
                End If
            Next

            Return res
        Finally
            If Not cmd Is Nothing Then
                If Not cmd.Connection Is Nothing Then
                    If cmd.Connection.State = ConnectionState.Open Then
                        cmd.Connection.Close()
                    End If
                End If
            End If
        End Try
    End Function

1 个答案:

答案 0 :(得分:2)

你的代码中有这个:

AppExecuteNonQuery(CommandType.StoredProcedure, "IT_Assets", , ht)

由于ht位于第4位,因此会将OutputParamTable分配给OUTPUT,这会将AppExecuteNonQuery(CommandType.StoredProcedure, "IT_Assets", ht) 个参数添加到存储过程中。这正是错误信息所说的内容。

要修复它,请将其移至位置3,即

{{1}}