【问题标题】:Parameter name is missing, While executing Function via code缺少参数名称,通过代码执行函数时
【发布时间】:2014-06-07 08:35:33
【问题描述】:

我正在尝试执行以下代码

 Dim transaction As PgSqlTransaction
    Dim command As New PgSqlCommand
    Dim paramLastNo As PgSqlParameter = command.Parameters.Add("funcUpdategtab03", PgSqlType.Int)
    command.Connection = Myconnstr
    Myconnstr.Open()
    command.Transaction = transaction
    transaction = command.Connection.BeginTransaction()
    Try
        command.CommandText = _
        "select funcUpdategtab03(" & SORD & "," & gintAcYrId & "," & gintBranchId & ");"
        command.ExecuteScalar()
        updateGtab03 = paramLastNo.Value
        transaction.Commit()

        Return updateGtab03
    Catch ex As Exception
        transaction.Rollback()
        MsgBox(ex.Message, MsgBoxStyle.Information, "RStari9 - updateGtab03")
    Catch ex As Exception
        transaction.Rollback()
        MsgBox(ex.Message, MsgBoxStyle.Information, "RStari9 - updateGtab03")
    Finally
        Myconnstr.Close()
    End Try

我创建的函数是

    CREATE OR REPLACE FUNCTION funcupdategtab03(ivrid integer, iacyrid integer, ibranchid integer)
  RETURNS integer AS
$BODY$
declare id_val int;
BEGIN
   UPDATE GTAB03 SET lastno=lastno+1 where vrid=ivrid and acyrid=iacyrid and BranchID=ibranchid RETURNING lastno into id_val;

return id_val;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
  • 上述函数用于更新表 得到的错误如下

错误参数名称丢失。

【问题讨论】:

  • 我想你还没有为 lastno+1 声明 lastno

标签: vb.net function postgresql stored-procedures


【解决方案1】:

您已经创建了一个输入参数并尝试将其用作输出参数。查询中根本没有使用该参数,因此无法获取值,并且查询实际返回的结果被忽略。

去掉参数,通过ExecuteScalar方法得到结果:

updateGtab03 = command.ExecuteScalar()

但是,您应该考虑对三个输入值使用参数,而不是将它们连接到查询中。

【讨论】:

  • updateGtab03 = (int)command.ExecuteScalar() 显示错误,但 updateGtab03 = command.ExecuteScalar 有效
  • @hector:我傻了,我在那里混入了一些 C#... 在 C# 中没有关闭严格的选项,所以它总是需要在那里进行转换。如果你在严格模式下编译 VB,你需要一个 CastDirectCast 来解开整数。
猜你喜欢
  • 1970-01-01
  • 2010-10-16
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多