我有3个表Worker
,Worker_issue
和Worker_receipt
Worker
主键为id
,Worker_issue
和Worker_receipt
有外键worker id
。
当worker_issue
的行数超过Worker_receipt
时,我会从查询中获得准确的结果,但当worker_receipt
的行数超过Worker_issue
时,我的结果为空查询。
以下是我在SQL Server 2008 Express中的存储过程
ALTER PROCEDURE dbo.WorkTDS
@id int
AS
WITH WorkerIssue AS (
SELECT
ROW_NUMBER() OVER (ORDER BY [Worker_issue_temp].id) RowNum, *
FROM
[Worker_issue_temp]
WHERE
[Worker_issue_temp].workerid = @id),
WorkerReceipt AS (
SELECT
ROW_NUMBER() OVER (ORDER BY [Worker_receipt_temp].id) RowNum, *
FROM
[Worker_receipt_temp]
WHERE
[Worker_receipt_temp].workerid = @id),
WorkerTable AS (
SELECT *
FROM [Worker])
SELECT
w.id AS ID,
w.labour AS Labour,
w.waste AS Waste,
IssueDate,
IssueParticular,
CASE WHEN IssueWeight IS NULL THEN 0 ELSE IssueWeight END AS IssueWeight,
CASE WHEN I_Rate IS NULL THEN 0 ELSE I_Rate END AS I_Rate,
ReceiptDate,
ReceiptParticular,
CASE WHEN R_Rate IS NULL THEN 0 ELSE R_Rate END AS R_Rate,
CASE WHEN ReceiptWeight IS NULL THEN 0 ELSE ReceiptWeight END AS ReceiptWeight
FROM
(SELECT
f.date AS IssueDate, f.id AS IssueID,
f.particular AS IssueParticular, f.rate AS I_Rate,
f.weight AS IssueWeight,
f.workerid AS IssueWorkerID,
u.weight AS ReceiptWeight, u.rate AS R_Rate, u.date AS ReceiptDate,
u.id AS ReceiptID, u.isDeleted AS ReceiptIsDeleted,
u.particular AS ReceiptParticular, u.weight,
u.workerid AS ReceiptWorkerID
FROM
WorkerIssue f
FULL OUTER JOIN
WorkerReceipt u ON u.RowNum = f.RowNum) AS abc,
WorkerTable AS w
WHERE
abc.ReceiptWorkerID = w.id
RETURN