如何为另一个数据表中的数据表上的每个循环

时间:2016-08-22 20:40:16

标签: sql-server vb.net foreach datatable

我有一个Web轮询应用程序,它从sql server后端读取和处理订单并插入到windss(jda)中。

流程

1)我读取了未处理的sql server表中的所有命令 2)然后我需要将订单分成两组    - 使用捆绑套件设置1个订单(带有多个子订单的1个订单)
- 第2组只是没有捆绑套件的订单 3)然后在处理每个订单后,我需要将isprocessed sql字段更新为1

1)阅读所有订单的代码

Frmmain.vb

 dt = WebDatabase.GetAllOrdersFromDatabase()**'this method is below**

 For Each drow In dt.Rows
    If CDbl(WinDSSStoreNumber) = 123 Or CDbl(WinDSSStoreNumber) = 124 Then 
     ' Store 123 and 124 = customer service - only orders with qualifying source codes

我的问题是:

以上每个检查商店是123还是124,但现在我想为每个循环实现另一个循环,读取数据表中第二个字段的所有InvoiceHeader_Id,然后检查它是否有捆绑套件,如果它然后处理它并在数据表中更新最后一个isprocessed字段处理订单而不处理任何捆绑包和更新的订单。如果您对我的任何帮助表示慷慨,请在下面的评论中向我提出任何问题。

WebData.vb

    Public Function GetAllOrdersFromDatabase() As DataTable
            DrivePath = "C:\Users\somjething\Documents\files\somefiles\"
            Dim ds As DataTable = Nothing
            WinDSS.connectionString = ConfigurationManager.AppSettings("WinDSS_Connection").Replace("%DrivePath%", DrivePath)
            ds = WinDSS.GetSysMst()

            If ds.Rows.Count > 0 Then
                WinDSSStoreNumber = ds.Rows(0)("store_no")

            End If
            Dim dt As New DataTable()
            Dim conn As New SqlConnection(ConfigurationManager.AppSettings("WebData_Connection") & ConfigurationManager.AppSettings("WebDataSource"))
            Dim cmd As SqlCommand
            Dim da As New SqlDataAdapter
            conn.Open()
            cmd = conn.CreateCommand()
            cmd.CommandType = CommandType.Text
        cmd.CommandText= SELECT InvoiceDetail_Id, InvoiceHeader_Id, ActualFreightCharge, AmountCollected, 
        ChargedActualFreight, CollectedExternally, CollectedThroughAR, DateInvoiced, InvoiceNo, 
        InvoiceType, LineSubTotal, MasterInvoiceNo, OrderInvoiceKey, Reference1, TotalAmount, 
        TotalTax, isprocessed, storetoprocess
            FROM dbo.InvoiceHeader 
        WHERE (isprocessed = 0) AND (storetoprocess = N'123')
  da.SelectCommand = cmd
        da.Fill(dt)
        conn.Close()

        Return dt
    End Function

添加信息

这是我的经理给我的信息:

循环遍历

SELECT InvoiceDetail_Id, InvoiceHeader_Id, ActualFreightCharge, AmountCollected, 
ChargedActualFreight, CollectedExternally, CollectedThroughAR, DateInvoiced, InvoiceNo, 
InvoiceType, LineSubTotal, MasterInvoiceNo, OrderInvoiceKey, Reference1, TotalAmount, 
TotalTax, isprocessed, storetoprocess
FROM dbo.InvoiceHeader WHERE (isprocessed = 0) AND (storetoprocess = N'195')

使用上面的InvoiceHeader_Id循环查看所有订单信息。处理每个Bundle(Kit /)

SELECT InvoiceHeader_Id, LineDetails_Id, LineDetail_Id, OrderLine_Id, GiftFlag, [References],
 GiftWrap, IsBundleParent, KitCode, KitQty, LevelOfService, LineSeqNo, LineType, 
MaxLineStatus, MaxLineStatusDesc, MinLineStatus, MinLineStatusDesc, MinShipByDate,
 OpenQty, OrderHeaderKey, OrderLineKey, OrderedQty, OriginalOrderedQty, OtherCharges,
 OverallStatus, PipelineKey, PrimeLineNo, ReceivingNode, RemainingQty, ReqCancelDate,
 ReqDeliveryDate, 
ReqShipDate, SCAC, ScacAndService, ScacAndServiceKey, ShipNode, ShipToID, ShipToKey,
 StatusQuantity, SubLineNo, SubstituteItemID, isprocessed
FROM dbo.OrderLine 
WHERE (isprocessed = 0) AND (InvoiceHeader_Id = 13) AND (IsBundleParent = 'Y')
ORDER BY PrimeLineNo, SubLineNo

对于上面列表查询中的每条记录,并添加为零(0)美元金额。成本与上述信息相关联(您可能需要查询表格以获取值) 从上面的记录中获取OrderLineKey并在下面查询以获取相关的子项。

SELECT TOP (100) PERCENT dbo.BundleParentLine.InvoiceHeader_Id AS BPL_InvoiceHeader_Id, 
dbo.BundleParentLine.LineDetails_Id AS BPL_LineDetails_Id, dbo.BundleParentLine.LineDetail_Id AS BPL_LineDetail_Id, dbo.BundleParentLine.OrderLine_Id AS BPL_OrderLine_Id, dbo.BundleParentLine.BundleParentLine_id, dbo.BundleParentLine.SubLineNo AS BPL_SubLineNo, dbo.BundleParentLine.PrimeLineNo AS BPL_PrimeLineNo, dbo.BundleParentLine.OrderLineKey AS BPL_OrderLineKey, dbo.OrderLine.*
FROM dbo.BundleParentLine INNER JOIN dbo.OrderLine ON dbo.BundleParentLine.OrderLine_Id = dbo.OrderLine.OrderLine_Id AND dbo.BundleParentLine.LineDetail_Id = dbo.OrderLine.LineDetail_Id AND dbo.BundleParentLine.LineDetails_Id = dbo.OrderLine.LineDetails_Id AND dbo.BundleParentLine.InvoiceHeader_Id = dbo.OrderLine.InvoiceHeader_Id
WHERE (dbo.BundleParentLine.OrderLineKey = N'76873264832') AND (dbo.BundleParentLine.InvoiceHeader_Id = 13) AND (dbo.BundleParentLine.LineDetails_Id = 6)  AND  (dbo.OrderLine.isprocessed = 0)
ORDER BY BPL_PrimeLineNo, BPL_SubLineNo

处理完毕后,设置isprocessed = 1

使用上面的InvoiceHeader_Id循环查看所有订单信息

SELECT InvoiceHeader_Id, LineDetails_Id, LineDetail_Id, OrderLine_Id, GiftFlag, [References],
 GiftWrap, IsBundleParent, KitCode, KitQty, LevelOfService, LineSeqNo, 
 LineType,
 MaxLineStatus, MaxLineStatusDesc, MinLineStatus, MinLineStatusDesc, MinShipByDate,
 OpenQty, OrderHeaderKey, OrderLineKey, OrderedQty, 
  OriginalOrderedQty, 
OtherCharges, OverallStatus, PipelineKey, PrimeLineNo, ReceivingNode, RemainingQty, 
ReqCancelDate, ReqDeliveryDate, ReqShipDate, SCAC, 
 ScacAndService,
 ScacAndServiceKey, ShipNode, ShipToID, ShipToKey, StatusQuantity, 
SubLineNo, SubstituteItemID, isprocessed
FROM   dbo.OrderLine
WHERE (isprocessed = 0) AND (InvoiceHeader_Id = 13) AND (IsBundleParent <> 'Y')
ORDER BY PrimeLineNo, SubLineNo

处理完毕后,设置isprocessed = 1

1 个答案:

答案 0 :(得分:0)

这不是一个答案,但我想提出这个问题,提示您做一些澄清,但在评论中这是不切实际的。

首先,关闭 visual studio并打开SQL Server Management Studio并连接到数据库

我们将构建一些select语句来获取数据然后我们可以考虑UPDATE语句

您发布的第一个查询为您提供商店195的未处理标题(直接来自您的帖子)。我不知道你是否想要为所有商店做这件事。将其粘贴到SSMS中并运行它

SELECT H.*
FROM dbo.InvoiceHeader H 
WHERE H.isprocessed = 0 AND H.storetoprocess = N'195'

现在,第二个查询为您提供了针对具有IsBundleParent =&#39; Y&#39;的标头的未处理行。

看!没有循环!

SELECT H.*, L.*
FROM dbo.InvoiceHeader H 
INNER JOIN
dbo.OrderLine L
ON L.InvoiceHeader_Id = H.InvoiceHeader_Id 
WHERE H.isprocessed = 0 AND H.storetoprocess = N'195'
AND L.isprocessed = 0 AND L.IsBundleParent = 'Y'

然后在你的解释中你有对于上面列表查询中的每条记录并添加为零(0)美元金额这对我没有意义所以也许你可以澄清

现在我们将添加第三个查询,其中添加了BundleParentLine(其中已经有内连接,但它是一个非常奇怪的连接字段列表)

SELECT H.*, L.*, BPL.*
FROM dbo.InvoiceHeader H 
INNER JOIN
dbo.OrderLine L
ON L.InvoiceHeader_Id = H.InvoiceHeader_Id 
INNER JOIN
dbo.BundleParentLine BPL
ON BPL.OrderLine_Id = L.OrderLine_Id 
AND BPL.LineDetail_Id = L.LineDetail_Id 
AND BPL.LineDetails_Id = L.LineDetails_Id 
AND BPL.InvoiceHeader_Id = L.InvoiceHeader_Id    
WHERE H.isprocessed = 0 AND H.storetoprocess = N'195'
AND L.isprocessed = 0 AND L.IsBundleParent = 'Y'

然后在你的解释中你有 After Processed后,设置isprocessed = 1 ,但是处理了什么&#39;这里?它只是将其设置为在表格中处理吗?

要做到这一点,你只需写一个UPDATE语句。这是一种方法,只需更新标题(不要运行它,直到我们了解您正在尝试做什么)。这里的问题是你想将标题和行设置为同时处理吗?

UPDATE InvoiceHeader
SET Processed = 1
WHERE isprocessed = 0 AND storetoprocess = N'195'

您的第四个查询似乎考虑了IsBundleParent <> 'Y'案例

同样,我们需要知道添加为零(0)美元金额已处理意味着更进一步。