這就是我的查詢內容,比較多,確實不知道怎麼寫存儲過程
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rs1 As New ADODB.Recordset
Dim SQL As String
Dim SQL1 As String
Dim count2 As Double '未交貨數量
conn.Open ConnectionString
SQL = "select OrderNo,CaseNo,TrustCaseNo,PlanDate,BookAmount from T01_ORDER_DTLS where OrderNo in (Select OrderNo from T01_ORDERS where Status ='2' or Status='1') and Status<>'E' order by PlanDate,CaseNo"
rs.Open SQL, conn, adOpenForwardOnly
LV.ListItems.Clear
While Not rs.EOF
LV.ListItems.Add , , IIf(IsNull(rs("CaseNo")), "", rs("CaseNo"))
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs("PlanDate")), "", rs("PlanDate"))
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs("BookAmount")), 0, rs("BookAmount"))
count2 = 0#
SQL1 = "select SUM(Pieces)as count1 from T13_OUTGOODS_DTLS where OutgoodNo in(Select OutgoodNo from T13_OUTGOODS where Status='E' and OrderKey='" & rs("OrderNo") & "') and CaseNo='" & rs("CaseNo") & "'"
rs1.Open SQL1, conn, adOpenForwardOnly
' SQL1 = "OutgoodNo in(Select OutgoodNo from T13_OUTGOODS where Status='E' and OrderKey='" & rs("OrderNo") & "') and CaseNo='" & rs("CaseNo") & "'"
' rs1.Find SQL1
If Not rs1.EOF Then
count2 = rs("BookAmount") - IIf(IsNull(rs1("count1")), 0, rs1("count1"))
End If
Set rs1 = Nothing
SQL1 = "Select BackAmt from T13_BACKGOOD_DTLS where OrderKey='" & rs("OrderNo") & "' and CaseNo='" & rs("CaseNo") & "'"
rs1.Open SQL1, conn, adOpenForwardOnly
If Not rs1.EOF Then
count2 = count2 + IIf(IsNull(rs1("BackAmt")), 0, rs1("BackAmt"))
End If
Set rs1 = Nothing
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , count2
' If count2 = 0 Then MsgBox rs("OrderNo"), vbExclamation, count2
SQL1 = "select Stocks from T12_STOCKS where CaseNo='" & rs("CaseNo") & "'"
rs1.Open SQL1, conn, adOpenForwardOnly
If rs1.EOF Then
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , 0
Else
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs1("Stocks")), 0, rs1("Stocks"))
End If
Set rs1 = Nothing
SQL1 = "select SUM(Pieces)as count from T13_OUTGOODS_DTLS where OutgoodNo in(Select OutgoodNo from T13_OUTGOODS where Status<>'E' and OrderKey='" & rs("OrderNo") & "') and CaseNo='" & rs("CaseNo") & "'"
rs1.Open SQL1, conn, adOpenForwardOnly
If Not rs1.EOF Then
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs1("count")), 0, rs1("count"))
Else
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , 0
End If
Set rs1 = Nothing
SQL1 = "select OrderNum from T01_ORDERS where OrderNo='" & rs("OrderNo") & "'"
rs1.Open SQL1, conn, adOpenForwardOnly
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs1("OrderNum")), "", rs1("OrderNum"))
Set rs1 = Nothing
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs("TrustCaseNo")), "", rs("TrustCaseNo"))
SQL1 = "select CustomNo,CustomName from T01_ORDERS where OrderNo='" & rs("OrderNo") & "'"
rs1.Open SQL1, conn, adOpenForwardOnly
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs1("CustomNo")), "", rs1("CustomNo"))
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs1("CustomName")), "", rs1("CustomName"))
Set rs1 = Nothing
SQL1 = "select MoldName from T01_CUSTOM_MOLDS where CaseNo='" & rs("CaseNo") & "'"
rs1.Open SQL1, conn, adOpenForwardOnly
If Not rs1.EOF Then
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs1("MoldName")), "", rs1("MoldName"))
Else
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , ""
End If
Set rs1 = Nothing
SQL1 = "select SUM(Pieces)as count1 from T13_OUTGOODS_DTLS where OutgoodNo in(Select OutgoodNo from T13_OUTGOODS where Status='E' and OrderKey='" & rs("OrderNo") & "') and CaseNo='" & rs("CaseNo") & "'"
rs1.Open SQL1, conn, adOpenForwardOnly
If Not rs1.EOF Then
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs1("count1")), 0, rs1("count1"))
Else
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , 0
End If
Set rs1 = Nothing
SQL1 = "select Address,Liaisons from T01_ORDERS where OrderNo='" & rs("OrderNo") & "'"
rs1.Open SQL1, conn, adOpenForwardOnly
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs1("Address")), "", Trim(rs1("Address")))
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , IIf(IsNull(rs1("Liaisons")), "", Trim(rs1("Liaisons")))
Set rs1 = Nothing
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , "訂單"
LV.ListItems(LV.ListItems.Count).ListSubItems.Add , , rs("OrderNo")
rs.MoveNext
Wend
Set rs = Nothing
labCT.Caption = LV.ListItems.Count
Set conn = Nothing