【问题标题】:Insert a row in a table based on the returned quantity of a function根据函数的返回数量在表中插入一行
【发布时间】:2015-04-18 19:33:57
【问题描述】:

我有一个返回计数的函数。使用这个返回的计数,我想为函数返回的每个 qty 插入一个新行到 SQL 表中。

这是我开始的示例:

compcount = ems.getparentcountfromorder(row("ordernumber"))

Dim count As Object = compcount

For Each i As Object In count
      connection.executesql("INSERT INTO composite_pick_ID (parentprodid, ordernumber) VALUES('" & row("productid") & "','" & row("ordernumber") & "')")
      count = count - 1
Next

我希望我的最终结果是这样的:

  • If count = 2,然后我想将 2 行插入到 composite_pick_ID

【问题讨论】:

  • 现在发生了什么?为什么将所有东西都转换为对象? compcount 的类型是什么?
  • 我们将 compcount 设置为整数,就使它们成为对象而言,当它是整数并且不知道该怎么做时,我无法遍历 count。
  • 只需for i = 0 to compcount-1Dim compcount as Integer

标签: sql-server vb.net tsql


【解决方案1】:

假设 ems.getparentcountfromorder(row("ordernumber")) 被编码为从 SQL 中返回,则维度 count 作为一个 Nullable 整数,以便在 SQL 作为查询结果返回 Null 时进行处理。

Dim count as Nullable(of Integer) = ems.getparentcountfromorder(row("ordernumber"))
'or in shorthand
Dim count as integer? = ems.getparentcountfromorder(row("ordernumber"))

If count.HasValue then
 For i as Integer = 1 to count
   connection.executesql('my insert statement')
 Next
End if

更多关于 VB 中的 Nullable 类型:Nullable Types in VB.NET?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-07
    • 2023-03-17
    • 1970-01-01
    • 2017-02-28
    • 2013-10-08
    • 2016-10-26
    • 2016-11-27
    • 2022-07-25
    相关资源
    最近更新 更多