vb table 适配器不允许在 IN 子句中使用多个参数
vb table adapter does not allow more than one parameter in the IN clause
我需要实现的是将未知数量的值列表发送到 Sql 服务器 NOT IN 子句,但只能使用奇异值来实现。以下是我的 Sql 声明:
SELECT SorMaster.LastInvoice
, SorMaster.SalesOrder
, SorMaster.OrderStatus
, ArCustomer.RouteCode
, SorMaster.Customer
, SorMaster.CustomerName
, SorMaster.CustomerPoNumber
, SorMaster.OrderDate
, SorMaster.DateLastInvPrt
, ArInvoice.InvoiceBal1
, ArInvoice.TermsCode
FROM SorMaster AS SorMaster
INNER JOIN ArCustomer AS ArCustomer ON SorMaster.Customer = ArCustomer.Customer
INNER JOIN ArInvoice AS ArInvoice ON SorMaster.LastInvoice = ArInvoice.Invoice
WHERE (SorMaster.OrderStatus = '9')
AND (SorMaster.Branch LIKE 'J%')
AND (SorMaster.DocumentType = 'O')
AND (SorMaster.LastInvoice > @Last_Invoice)
AND (SorMaster.OrderDate > DATEADD(Month, - 4, GETDATE()))
AND (SorMaster.LastInvoice NOT IN (@ExclusionList))
ORDER BY SorMaster.LastInvoice
@ExclusionList 值由此代码生成为来自列表框的字符串:
Dim exclusion As String = ""
If MenuForm.ExclusionCB.Checked = True Then
For i = 0 To MenuForm.ExclusionLB.Items.Count - 2
exclusion = exclusion & MenuForm.ExclusionLB.Items(i) & ","
Next
exclusion = exclusion & MenuForm.ExclusionLB.Items(MenuForm.ExclusionLB.Items.Count - 1)
Else
exclusion = ""
End If
我也试过将整个列表框作为一个集合发送。
有谁知道我如何发送多个值(例如 1,2,3,4,5,6)并让 sql 明白这些值不止一个? SELECT 声明更改不会有问题,只要它 returns 相同的信息即可。
我需要这个和例外列表的原因是我们的远程数据库 PK 在 Salesorder 列上,而本地数据库在 LastInvoice 列上
希望这是有道理的。如果您需要更多信息,请告诉我
您可以将其作为字符串发送并使用动态 sql。这是一个简单的例子,如何做到这一点。
DECLARE @vals VARCHAR(50) = '1,2,3,4,5,6'
DECLARE @sql VARCHAR(MAX) = 'SELECT * FROM TABLE WHERE FIELD1 IN'
SET @sql = @sql + ' (' + @vals + ')'
-- @sql = 'SELECT * FROM TABLE WHERE FIELD1 IN (1,2,3,4,5,6)'
EXEC (@sql)
我需要实现的是将未知数量的值列表发送到 Sql 服务器 NOT IN 子句,但只能使用奇异值来实现。以下是我的 Sql 声明:
SELECT SorMaster.LastInvoice
, SorMaster.SalesOrder
, SorMaster.OrderStatus
, ArCustomer.RouteCode
, SorMaster.Customer
, SorMaster.CustomerName
, SorMaster.CustomerPoNumber
, SorMaster.OrderDate
, SorMaster.DateLastInvPrt
, ArInvoice.InvoiceBal1
, ArInvoice.TermsCode
FROM SorMaster AS SorMaster
INNER JOIN ArCustomer AS ArCustomer ON SorMaster.Customer = ArCustomer.Customer
INNER JOIN ArInvoice AS ArInvoice ON SorMaster.LastInvoice = ArInvoice.Invoice
WHERE (SorMaster.OrderStatus = '9')
AND (SorMaster.Branch LIKE 'J%')
AND (SorMaster.DocumentType = 'O')
AND (SorMaster.LastInvoice > @Last_Invoice)
AND (SorMaster.OrderDate > DATEADD(Month, - 4, GETDATE()))
AND (SorMaster.LastInvoice NOT IN (@ExclusionList))
ORDER BY SorMaster.LastInvoice
@ExclusionList 值由此代码生成为来自列表框的字符串:
Dim exclusion As String = ""
If MenuForm.ExclusionCB.Checked = True Then
For i = 0 To MenuForm.ExclusionLB.Items.Count - 2
exclusion = exclusion & MenuForm.ExclusionLB.Items(i) & ","
Next
exclusion = exclusion & MenuForm.ExclusionLB.Items(MenuForm.ExclusionLB.Items.Count - 1)
Else
exclusion = ""
End If
我也试过将整个列表框作为一个集合发送。
有谁知道我如何发送多个值(例如 1,2,3,4,5,6)并让 sql 明白这些值不止一个? SELECT 声明更改不会有问题,只要它 returns 相同的信息即可。
我需要这个和例外列表的原因是我们的远程数据库 PK 在 Salesorder 列上,而本地数据库在 LastInvoice 列上
希望这是有道理的。如果您需要更多信息,请告诉我
您可以将其作为字符串发送并使用动态 sql。这是一个简单的例子,如何做到这一点。
DECLARE @vals VARCHAR(50) = '1,2,3,4,5,6'
DECLARE @sql VARCHAR(MAX) = 'SELECT * FROM TABLE WHERE FIELD1 IN'
SET @sql = @sql + ' (' + @vals + ')'
-- @sql = 'SELECT * FROM TABLE WHERE FIELD1 IN (1,2,3,4,5,6)'
EXEC (@sql)