0
SELECT [Acc_DocumentNo],[Acc_DocumentDate],[Acc_DocumentType],[Acc_DocumentRef],[Acc_DocumentAmount],
    [Acc_CustomerID],[Acc_DebtorGroupID],[Acc_DebtorBranchID],
    (SELECT Acc_ManualReferenceNo
        FROM Acc_CreditDocuments
        WHERE Acc_DocumentNo IN 
        (SELECT Acc_DocumentRef FROM Acc_CreditDocuments 
    WHERE Acc_DocumentNo = @DocumentNo)) As [Acc_ManualReferenceNo],
        [Acc_Status],[Acc_CreatedBy],[Acc_CreatedAt],[Acc_ModifiedBy],[Acc_ModifiedAt]
FROM Acc_CreditDocuments
WHERE Acc_DocumentNo = @DocumentNo AND
Acc_DocumentRef <> Acc_DocumentNo

在上述查询中,用于选择的子查询语句Acc_ManualReferenceNo如果有多个值,则返回多个值DocumentNo

我可以打开数据库游标来一一获取和插入值吗?或者有没有其他方法可以做到这一点?

任何帮助都感激不尽!

4

1 回答 1

1

JOIN表本身,而不是,像这样:

SELECT 
  a1.[Acc_DocumentNo],
  a1.[Acc_DocumentDate],
  a1.[Acc_DocumentType],
  a1.[Acc_DocumentRef],
  a1.[Acc_DocumentAmount],
  a1.[Acc_CustomerID],
  a1.[Acc_DebtorGroupID],
  a1.[Acc_DebtorBranchID],
  a2.Acc_ManualReferenceNo As [Acc_ManualReferenceNo] -- < --
  ...
FROM Acc_CreditDocuments AS a1
INNER JOIN Acc_CreditDocuments AS a2 ON a1.cc_DocumentRef = a2.Acc_DocumentNo
WHERE Acc_DocumentNo = @DocumentNo 
  AND a1.Acc_DocumentRef <> a1.Acc_DocumentNo
于 2013-09-10T08:20:22.373 回答