根据返回的函数数量在表中插入一行

时间:2015-04-18 19:33:58

标签: sql-server vb.net tsql

我有一个返回计数的函数。使用此返回的计数,我想为函数返回的每个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,然后我想在composite_pick_ID中插入2行。

1 个答案:

答案 0 :(得分:0)

假设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?