下面的查询在PL / Sql中运行正常,但我想在MS Sql Server中运行..任何人都可以帮我。
SELECT ARM_KEYID, ARD_KEYID, FCT_FACTORYID, FCT_NAME, AUC_AUDITTYPEID, ATM_DESCRIPT,
LIN_LINEID, LIN_DESCRIPTION, CEL_CELLID, CEL_DESCRIPTION,ARM_EMPID,
AUDITOR.EMP_NAME, ARM_REPDATE, AUC_WEEKNO,AUC_FROMDATE, AUC_TILLDATE,
(SELECT OPD_DESCRIPT
FROM OPERATIONSDETAILS OPD
WHERE OPD.OPD_KEYID = OPDDETAILS.OPD_PARENTID),
OPD_OPNO, OPD_DESCRIPT , CLD_OPNO, CLD_DESCRIPT,
ALM_KEYID,DECODE(ALD_PRIORITY,'H','High','M','Medium','L','Low') ALD_PRIORITY,
ARD_FEEDBACK,ARD_NCCLASSIFICATION,ALD_NCACTION,OWNER.EMP_NAME,
dpt_description , ALD_TARGETDATE, COMPLETED.EMP_NAME, ALD_COMPLETIONDATE,
ALD_COMPREMARKS , DECODE(ALD_STATUS,'Y',ROUND(ALD_COMPLETIONDATE-ALD_TIMESTAMP),
ROUND(SYSDATE-ALD_TIMESTAMP)) AS PENDING_DAYS,
DECODE(ALD_STATUS,'Y','COMPLETED','N','PENDING') AS LAD_STATUS
FROM AUDITREPORTMASTER, AUDITREPORTDETAILS, CHECKLISTDETAILS, AUDITCALENDAR, OPERATIONSMASTER,
OPERATIONSDETAILS OPDDETAILS, ACTIONLISTMASTER, ACTIONLISTDETAILS,
AUDITTYPEMASTER, CELLMASTER, EMPLOYEEMASTER AUDITOR, EMPLOYEEMASTER
OWNER, EMPLOYEEMASTER COMPLETED , FACTORYMASTER,LINEMASTER , departmentmaster
WHERE ARD_NCSTATUS = 'N'
AND ARD_RECNO ='1'
AND AUC_ACTIVE='Y'
AND ARM_KEYID = ARD_MASTERID
AND CLD_KEYID = ARD_CKDETAILID
AND AUC_KEYID = ARM_CALENDARID
AND AUC_OPDKEYID = OPD_KEYID
AND OPM_KEYID = OPD_OPMASTERID
AND ATM_KEYID = AUC_AUDITTYPEID
AND CEL_CELLID = AUC_LINEID
AND LIN_LINEID = AUC_SECTIONID
AND ARM_EMPID = AUDITOR.EMP_EMPLOYEEID
AND ALD_OWNER = OWNER.EMP_EMPLOYEEID (+)
AND DPT_DEPARTMENTID(+) = OWNER.emp_departmentid
AND ALD_COMPLETEDBY = COMPLETED.EMP_EMPLOYEEID (+)
AND FCT_FACTORYID = AUC_FACTORYID
AND ALD_AUDITDETAILID (+) = ARD_KEYID
AND ALM_KEYID (+) = ALD_MASTERID
AND ALD_STATUS IS NULL AND ARM_ACTIVE ='Y' AND AUC_AUDITTYPEID = 'ATM/01'
ORDER BY FCT_FACTORYID, OPM_AUDITTYPEID, CEL_CELLID,ARM_REPDATE;
答案 0 :(得分:0)
如果您有更好的格式和更明确的连接语法和命名约定而不是隐式连接和命名,它将使代码更具可读性和可重写性。您的代码应该看起来像这样。
注意:这不是代码的工作版本,因为我不得不做出很多假设,因为缺少明确的列/表命名,但你知道需要做什么。
SELECT ARM_KEYID,
ARD_KEYID,
FCT_FACTORYID,
FCT_NAME,
AUC_AUDITTYPEID,
ATM_DESCRIPT,
LIN_LINEID,
LIN_DESCRIPTION,
CEL_CELLID,
CEL_DESCRIPTION,
ARM_EMPID,
AUDITOR.EMP_NAME,
ARM_REPDATE,
AUC_WEEKNO,
AUC_FROMDATE,
AUC_TILLDATE,
OPD.OPD_DESCRIPT,
OPD_OPNO,
OPD_DESCRIPT,
CLD_OPNO,
CLD_DESCRIPT,
ALM_KEYID,
CASE
WHEN ALD_PRIORITY='H' THEN 'High'
WHEN ALD_PRIORITY='M' THEN 'Medium'
WHEN ALD_PRIORITY='L' THEN 'Low'
END AS ALD_PRIORITY,
ARD_FEEDBACK,
ARD_NCCLASSIFICATION,
ALD_NCACTION,
OWNER.EMP_NAME,
dpt_description,
ALD_TARGETDATE,
COMPLETED.EMP_NAME,
ALD_COMPLETIONDATE,
ALD_COMPREMARKS,
CASE
WHEN ALD_STATUS='Y' THEN ROUND(ALD_COMPLETIONDATE-ALD_TIMESTAMP,ROUND(SYSDATE-ALD_TIMESTAMP,1)) --ROUND NEEDS ATLEAST 2 ATTRIBUTES
END AS PENDING_DAYS,
CASE
WHEN ALD_STATUS='Y' THEN 'COMPLETED'
WHEN ALD_STATUS='N' THEN 'PENDING'
END AS LAD_STATUS,
FROM AUDITREPORTMASTER ARM
RIGHT OUTER JOIN OWNER ON ARM.ALD_OWNER = OWNER.EMP_EMPLOYEEID
LEFT OUTER JOIN ARM.DPT_DEPARTMENTID = OWNER.emp_departmentid
JOIN EMPLOYEEMASTER AUDITOR ON ARM.ARM_EMPID = AUDITOR.EMP_EMPLOYEEID
JOIN AUDITREPORTDETAILS ARD ON
ARM.ARM_KEYID = ARD.ARD_MASTERID
-- Add all the other joins and if the (+) is before the = its left outer join else its right outer join
WHERE ARD_NCSTATUS = 'N'
AND ARD_RECNO ='1'
AND AUC_ACTIVE='Y'
AND ALD_STATUS IS NULL
AND ARM_ACTIVE ='Y'
AND AUC_AUDITTYPEID = 'ATM/01'
ORDER BY FCT_FACTORYID, OPM_AUDITTYPEID, CEL_CELLID,ARM_REPDATE;