遍历更多“混淆的”代码并进行重构以供理解。以为这是直截了当的,直到我打了第三份案子。我可以假定带引号的列引用的是t_g3视图吗?
原始的FROM部分在下面发布。
FROM
[GCCC_ProjectServer_Reporting].dbo.MSP_EpmProject_UserView AS p
INNER JOIN
(SELECT
t_g3.ProjectUID as ProjectUID
,CASE WHEN @vLastBaseline = 'BL1' AND t_g3.TaskBaseline1StartDate IS NOT NULL THEN t_g3.TaskBaseline1StartDate
WHEN @vLastBaseline = 'BL1' AND t_g3.TaskBaseline1StartDate IS NULL THEN t_g3.TaskBaseline5StartDate
WHEN @vLastBaseline = 'BL2' AND t_g3.TaskBaseline2StartDate IS NOT NULL THEN t_g3.TaskBaseline2StartDate
WHEN @vLastBaseline = 'BL2' AND t_g3.TaskBaseline2StartDate IS NULL THEN t_g3.TaskBaseline5StartDate
WHEN @vLastBaseline = 'BL3' AND t_g3.TaskBaseline3StartDate IS NOT NULL THEN t_g3.TaskBaseline3StartDate
WHEN @vLastBaseline = 'BL3' AND t_g3.TaskBaseline3StartDate IS NULL THEN t_g3.TaskBaseline5StartDate
WHEN @vLastBaseline = 'BL4' AND t_g3.TaskBaseline4StartDate IS NOT NULL THEN t_g3.TaskBaseline4StartDate
WHEN @vLastBaseline = 'BL4' AND t_g3.TaskBaseline4StartDate IS NULL THEN t_g3.TaskBaseline5StartDate
ELSE t_g3.TaskBaseline1StartDate
END AS g3_LastBaseline
,CASE WHEN @vLastBaseline = 'BL1' AND t_cs.TaskBaseline1StartDate IS NOT NULL THEN t_cs.TaskBaseline1StartDate
WHEN @vLastBaseline = 'BL1' AND t_cs.TaskBaseline1StartDate IS NULL THEN t_cs.TaskBaseline5StartDate
WHEN @vLastBaseline = 'BL2' AND t_cs.TaskBaseline2StartDate IS NOT NULL THEN t_cs.TaskBaseline2StartDate
WHEN @vLastBaseline = 'BL2' AND t_cs.TaskBaseline2StartDate IS NULL THEN t_cs.TaskBaseline5StartDate
WHEN @vLastBaseline = 'BL3' AND t_cs.TaskBaseline3StartDate IS NOT NULL THEN t_cs.TaskBaseline3StartDate
WHEN @vLastBaseline = 'BL3' AND t_cs.TaskBaseline3StartDate IS NULL THEN t_cs.TaskBaseline5StartDate
WHEN @vLastBaseline = 'BL4' AND t_cs.TaskBaseline4StartDate IS NOT NULL THEN t_cs.TaskBaseline4StartDate
WHEN @vLastBaseline = 'BL4' AND t_cs.TaskBaseline4StartDate IS NULL THEN t_cs.TaskBaseline5StartDate
ELSE t_cs.TaskBaseline1StartDate
END AS cs_LastBaseline
,CASE WHEN @vLastBaseline = 'BL1' AND t_g3.TaskBaseline1StartDate IS NOT NULL THEN 'TaskBaseline1StartDate'
WHEN @vLastBaseline = 'BL1' AND t_g3.TaskBaseline1StartDate IS NULL THEN 'TaskBaseline5StartDate'
WHEN @vLastBaseline = 'BL2' AND t_g3.TaskBaseline2StartDate IS NOT NULL THEN 'TaskBaseline2StartDate'
WHEN @vLastBaseline = 'BL2' AND t_g3.TaskBaseline2StartDate IS NULL THEN 'TaskBaseline5StartDate'
WHEN @vLastBaseline = 'BL3' AND t_g3.TaskBaseline3StartDate IS NOT NULL THEN 'TaskBaseline3StartDate'
WHEN @vLastBaseline = 'BL3' AND t_g3.TaskBaseline3StartDate IS NULL THEN 'TaskBaseline5StartDate'
WHEN @vLastBaseline = 'BL4' AND t_g3.TaskBaseline4StartDate IS NOT NULL THEN 'TaskBaseline4StartDate'
WHEN @vLastBaseline = 'BL4' AND t_g3.TaskBaseline4StartDate IS NULL THEN 'TaskBaseline5StartDate'
ELSE 't_cs.TaskBaseline1StartDate'
END AS LastBaselineSource
FROM GCCC_ProjectServer_Reporting.dbo.MSP_EpmTask_UserView AS t_g3
LEFT JOIN
GCCC_ProjectServer_Reporting.dbo.MSP_EpmTask_UserView AS t_cs
ON t_g3.ProjectUID = t_cs.ProjectUID
AND t_cs.TaskIsActive = 1
AND t_cs.TaskName = 'Construction Start'
WHERE t_g3.TaskIsActive = 1
AND t_g3.TaskName = 'Gate 3 Approval'
) as BL
ON p.ProjectUID = BL.ProjectUID
在这里编码
另一个示例,其中单引号用作列别名包装。在整个代码库中非常常见。这就是为什么我认为上述问题与列别名有关。
SELECT p0.[Project No],
p0.ProjectName,
isnull(p0.Budg_Centre + ' - ' + p0.Budg_CentreName,' - ') as 'BudgetCentre',
p0.Budg_Centre,p0.Budg_CentreName,
CASE WHEN DATEDIFF(d,p0.ProjectStartDate,p0.ProjectFinishDate) > 40
THEN 'Scheduled'
ELSE 'Unscheduled' END as 'Schedule Status',
CapFor.Forecasting_Month,
CapFor.Forecasting_Year,
CapFor.FinancialYr,
p0.ProjectOwnerName,
答案 0 :(得分:1)
第3种情况与第一种情况重复的逻辑相同,但报告的是第一种情况选择的列作为字符串。
单引号('
)仅是字符串文字定界符 1 。
双引号("
)可用作字符串定界符或用于引用标识符(与[]
可以用于引用标识符的方式相同)。应该如何解释它们取决于QUOTED_IDENTIFIER
的当前设置。
所以,不要用列引用替换字符串。
1 当我做出如此自信的断言时,我注定会忘记一些极端情况,不是吗。