无法绑定由多个部分组成的标识符
--4.药房发药单
SELECT
B.BCK01, A.BillNO, A.DeptID
, CASE WHEN A.DeptID=0 THEN '零售' ELSE (SELECT BCK03 FROM BCK1 WHERE BCK1.BCK01 = A.DeptID) END 部门名称
, B.VAA01, B.VAJ01, B.BBY01, Y.BBY04, Y.BBY05, Y.BBY06, B.Unit, B.PackSize, B.BatchNo
, B.Quantity AS Quantity4, B.PurchasePrice AS PurchasePrice4, B.PurchaseAmount AS PurchaseAmount4
, K.PresellPrice AS PresellPrice4, K.PresellPrice * B.Quantity AS PresellAmount4
, B.VAJ33 AS VAJ33A, B.VAJ38 AS VAJ38A
, 0 AS Quantity5, 0 AS PurchasePrice5, 0 AS PurchaseAmount5
, 0 AS PresellPrice5, 0 AS PresellAmount5
, 0 AS VAJ33B, 0 AS VAJ38B
, A.AuditingDate, A.Assessor
, A.Accepter, A.Dispenser, A.Sender, A.Ratifier, A.TradeID, B.DPK_ID, B.Remark
, Y.BBE02, T.BAP02, Q.BAX03, O.BDO03, G.BAG15
, K.ProduceDate, K.ExpiryDate
,F.VAA05
INTO #TMP_DPD
FROM DPD1 A join DPD2 B ON A.BCK01 = B.BCK01 and A.BillID = B.BillID
join VAA1 F ON F.VAA01 = B.VAA01
JOIN BBY1 Y ON Y.BBY01 = B.BBY01
left join BAX1 Q ON Q.BAX01 = Y.BAX01
join BAG1 G ON G.BBY01 = B.BBY01
left join BBX1 X ON X.BBX01 = G.BBX01
left join BBT1 T ON T.BBX01 = X.BBX01
left join BDO1 O ON O.BDO01 = X.BDO01
JOIN DPK1 K ON K.ID = B.DPK_ID
WHERE B.BCK01 = @PharmacyId
and B.Quantity > 0
and A.AuditingDate BETWEEN @BeginDate AND @EndDate
AND Y.BBY05 like @BBY05 + '%'
UNION ALL
SELECT
B.BCK01, A.BillNO, A.DeptID
, CASE WHEN A.DeptID=0 THEN '零售' ELSE (SELECT BCK03 FROM BCK1 WHERE BCK1.BCK01 = A.DeptID) END 部门名称
, B.VAA01, B.VAJ01, B.BBY01, Y.BBY04, Y.BBY05, Y.BBY06, B.Unit, B.PackSize, B.BatchNo
, B.Quantity AS Quantity4, B.PurchasePrice AS PurchasePrice4, B.PurchaseAmount AS PurchaseAmount4
, K.PresellPrice AS PresellPrice4, K.PresellPrice * B.Quantity AS PresellAmount4
, B.VAJ33 AS VAJ33A, B.VAJ38 AS VAJ38A
, 0 AS Quantity5, 0 AS PurchasePrice5, 0 AS PurchaseAmount5
, 0 AS PresellPrice5, 0 AS PresellAmount5
, 0 AS VAJ33B, 0 AS VAJ38B
, A.AuditingDate, A.Assessor
, A.Accepter, A.Dispenser, A.Sender, A.Ratifier, A.TradeID, B.DPK_ID, B.Remark
, Y.BBE02, T.BAP02, Q.BAX03, O.BDO03, G.BAG15
, K.ProduceDate, K.ExpiryDate
,F.VAA05
FROM DPD1_2 A join DPD2_2 B ON A.BCK01 = B.BCK01 and A.BillID = B.BillID
join VAA1 F ON F.VAA01 = B.VAA01 JOIN BBY1 Y ON Y.BBY01 = B.BBY01
left join BAX1 Q ON Q.BAX01 = Y.BAX01
join BAG1 G ON G.BBY01 = B.BBY01
left join BBX1 X ON X.BBX01 = G.BBX01
left join BBT1 T ON T.BBX01 = X.BBX01
left join BDO1 O ON O.BDO01 = X.BDO01
JOIN DPK1 K ON K.ID = B.DPK_ID
WHERE B.BCK01 = @PharmacyId
and B.Quantity > 0
and A.AuditingDate BETWEEN @BeginDate AND @EndDate
AND Y.BBY05 like @BBY05 + '%'
--5.药房退药单
UNION ALL
SELECT
B.BCK01, A.BillNO, A.DeptID
, CASE WHEN A.DeptID=0 THEN '零售' ELSE (SELECT BCK03 FROM BCK1 WHERE BCK1.BCK01 = A.DeptID) END 部门名称
, B.VAA01, B.VAJ01, B.BBY01, Y.BBY04, Y.BBY05, Y.BBY06, B.Unit, B.PackSize, B.BatchNo
, 0, 0, 0, 0, 0
, 0, 0
, -B.Quantity, B.PurchasePrice, -B.PurchaseAmount, K.PresellPrice, -K.PresellPrice * B.Quantity
, B.VAJ33 AS VAJ33B, B.VAJ38 AS VAJ38B
, A.AuditingDate, A.Assessor
, A.Accepter, A.Dispenser, A.Sender, A.Ratifier, A.TradeID, B.DPK_ID, B.Remark
, Y.BBE02, T.BAP02, Q.BAX03, O.BDO03, G.BAG15
, K.ProduceDate, K.ExpiryDate
,F.VAA05
FROM DPD1 A join DPD2 B ON A.BCK01 = B.BCK01 and A