以下SQL查找找到符合某些要求的订单,然后在此阶段创建一个带有发票行(_btblInvoiceLines)的新订单(InvNum),它只创建了基本信息。
DECLARE @gid varchar(50) SELECT @gid = newid()
INSERT INTO InvNum (DocType,DocState,AccountID,ucIDSOrdTempInvID)
SELECT 4,1,AccountID,@gid
FROM (SELECT DISTINCT AccountID
FROM InvNum
WHERE ubIDSOrdConsolOrder = '1'
AND DocState = '1'
AND DocType <> '5') A
INSERT INTO _btblInvoiceLines (iInvoiceID, iStockCodeID, fQuantity)
SELECT AutoIndex, iStockCodeID, qty
FROM (SELECT AutoIndex, iStockCodeID, qty
FROM (SELECT AutoIndex,AccountId,ucIDSOrdTempInvID
FROM InvNum
WHERE ucIDSOrdTempInvID = @gid) T1
INNER JOIN (SELECT B.iStockCodeID,A.AccountID,sum(B.fQuantity) AS qty
FROM (SELECT *
FROM InvNum
WHERE ubIDSOrdConsolOrder = '1'
AND DocState = '1'
AND DocType <> '5') A
INNER JOIN (SELECT *
FROM _btblInvoiceLines) B ON A.AutoIndex = B.iInvoiceID
GROUP BY iStockCodeID, AccountID) T2 ON T1.AccountId = T2.AccountID) T3
此后,InvNum列全部更新,这样可以正常工作。我的问题是以下内容,即更新新订单的_btblInvoiceLines和新订单的新行,匹配:WHERE ubIDSOrdConsolOrder = '1' AND DocState = '1' AND DocType <> '5'
这个FROM子句是不正确的:
SELECT D1.*
FROM (SELECT A.*
FROM _btblInvoiceLines A
INNER JOIN (SELECT max(idInvoiceLines) as idInvoiceLines, iStockCodeID
FROM _btblInvoiceLines
GROUP BY iStockCodeID) B ON A.idInvoiceLines = B.idInvoiceLines) D1
INNER JOIN (SELECT *
FROM _btblInvoiceLines I1
INNER JOIN (SELECT accountid,autoindex,ucIDSOrdTempInvID
FROM InvNum
WHERE ucIDSOrdTempInvID = @gid) I2 ON I1.iInvoiceID = I2.AutoIndex) D2 ON D1.iStockCodeID = D2.iStockCodeID
WHERE _btblInvoiceLines.idInvoiceLines = D2.idInvoiceLines
我迷失了如何制作D1。*从最新的原始_btblInvoiceLines中提取信息,该信息被添加到新订单D2中。我认为我能做到这一点的唯一方法是使用以下SELECT DISTINCT AccountID,ubIDSOrdConsolOrder FROM InvNum WHERE ubIDSOrdConsolOrder = '1' AND DocState = '1' AND DocType <> '5'
,但我不知道如何正确地使用它。很抱歉,如果这是一个简单的问题,但我一直在寻找并尝试解决此问题2天,现在没有任何运气。
谢谢。
答案 0 :(得分:1)
我的INNER JOIN
顺序错误。我做了以下操作,现在可以正常工作了:
SELECT D1.*
FROM (SELECT DISTINCT AccountID, AutoIndex FROM InvNum WHERE ubIDSOrdConsolOrder = '1' AND DocState = '1' AND DocType <> '5') A1
INNER JOIN (SELECT * FROM _btblInvoiceLines) D1 ON A1.AutoIndex = D1.iInvoiceID----
INNER JOIN (SELECT max(idInvoiceLines) as idInvoiceLines, iStockCodeID FROM _btblInvoiceLines GROUP BY iStockCodeID) I1 ON D1.iStockCodeID = I1.iStockCodeID
INNER JOIN (SELECT * FROM _btblInvoiceLines I1 INNER JOIN (select accountid,autoindex,ucIDSOrdTempInvID FROM InvNum where ucIDSOrdTempInvID = @gid) I2 ON I1.iInvoiceID=I2.AutoIndex) D2 ---- @gid
ON D1.iStockCodeID=D2.iStockCodeID
WHERE _btblInvoiceLines.idInvoiceLines = D2.idInvoiceLines