SQL Query随机停止工作?

时间:2013-05-02 13:50:55

标签: sql sql-server-2008 reporting-services union

我有一个SQL查询,提供SSRS报告的信息。该报告今天停止工作,我检查了查询,但它也没有工作。我看不出查询有什么问题,但它不会运行。任何帮助将不胜感激,我真的需要今天在线恢复此报告,这是产生的错误:

Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Expr2522'.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1172.ShortDescription" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1170.ShortDescription" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1168.ShortDescription" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.Exam" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.PVHigh" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.PVAch" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.PVComp" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.PVCont" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.PVFund" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.PVXfr" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.PVStart" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.PG_DifficultyOrDisabilityID" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.PG_EthnicityID" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.PG_SexID" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.ExpEndYrNumber" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Tbl1166.LR_MYSID" could not be bound.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col5840'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col5843'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Col5849'.

这是我的问题:

WITH All_Genders_LLDDs_Ethns as (SELECT
                                 Gender,
                                 LLDD,
                                 Ethnicity,
                                 PG_AgeBandID,
                                 AgeBand,
                                 AgeGroup

                                 FROM
                                 (SELECT 'Male' as Gender 
                                  UNION ALL SELECT 'Female' as Gender) as Genders,
                                 (SELECT 'LLDD' as LLDD 
                                  UNION ALL SELECT 'NO_LLDD' as LLDD) as LLDDs,
                                 (SELECT 'WB' as Ethnicity
                                  UNION ALL SELECT 'NWB' as Ethnicity) as Ethnicities,
                                 (SELECT DISTINCT
                                  PG_AgeBandID,
                                  ab.Description as AgeBand,
                                  ag.Description as AgeGroup

                                  FROM
                                  MSQL2.ProAchieve.dbo.LR_MYSHighByLevel l
                                  LEFT JOIN MSQL2.ProGeneral.dbo.AgeBand ab on l.PG_AgeBandID = ab.AgeBandID
                                  LEFT JOIN MSQL2.ProGeneral.dbo.AgeGroup ag on l.PG_AgeGroupID = ag.AgeGroupID

                                  WHERE
                                  l.PG_QType3ID not in ('30','40') and
                                  l.LR_MYSID in (SELECT 
                                                   Setting
                                                  FROM 
                                                  msql2.ProAchieve.dbo.SystemSetting
                                                  WHERE 
                                                  Code='DefaultLRSummary') AND 
                                  l.PG_ExpEndYrID IN ('11/12')) as AGE_Bands

                                 ),
    CoreProAchData as (SELECT
                        al.AgeBand,
                        al.AgeGroup,
                        lml.LR_MYSID,
                        al.Gender,
                        al.LLDD,
                        al.Ethnicity,
                        lml.EthnicityDetails,
                        lml.LDDetails,
                        lml.DisabilityDetails,
                        '' AS Details,
                        al.PG_AgeBandID,
                        lml.PG_ExpEndYrID,
                        lml.ExpEndYrNumber,
                        sum(lml.pvstart) as pvstart,
                        sum(lml.PVXfr) as PVXfr,
                        sum(lml.PVFund) as PVFund,
                        sum(lml.PVCont) as PVCont,
                        sum(lml.PVComp) as PVComp,
                        sum(lml.PVAch) as PVAch,
                        sum(lml.PVHigh) as PVHigh,
                        sum(lml.exam) as exam

                        FROM
                        All_Genders_LLDDs_Ethns al
                        LEFT JOIN (SELECT
                                   CASE WHEN l.PG_AgeGroupID = '1' THEN '16-18' ELSE '19+' END as AgeGroup,  
                                   CASE WHEN l.PG_SexID = 'M' THEN 'Male' ELSE 'Female' END as Gender,
                                   CASE WHEN l.PG_EthnicityID IN ('23','31') THEN 'WB' ELSE 'NWB' END as Ethnicity,
                                   CASE WHEN l.PG_DifficultyOrDisabilityID IN ('1') THEN 'LLDD' ELSE 'NO_LLDD' END as LLDD,
                                   e.ShortDescription AS EthnicityDetails,
                                   ld.ShortDescription AS LDDetails,
                                   d.ShortDescription AS DisabilityDetails,
                                   l.*

                                   FROM
                                   MSQL2.ProAchieve.dbo.LR_MYSHighByLevel l
                                   INNER JOIN MSQL2.ProGeneral.dbo.Ethnicity e ON e.EthnicityID = l.PG_EthnicityID
                                   INNER JOIN MSQL2.ProGeneral.dbo.LearningDifficulty ld ON ld.LearningDifficultyID = l.PG_LearningDifficultyID
                                   INNER JOIN MSQL2.ProGeneral.dbo.Disability d ON d.DisabilityID = l.PG_DisabilityID

                                   WHERE
                                   l.PG_QType3ID not in ('30','40') and
                                   l.LR_MYSID in (SELECT 
                                                  Setting
                                                  FROM 
                                                  msql2.ProAchieve.dbo.SystemSetting
                                                  WHERE 
                                                  Code='DefaultLRSummary') AND 
                                   l.PG_ExpEndYrID IN ('11/12')
                                   )lml on al.Gender = lml.Gender and
                                           al.LLDD = lml.LLDD and
                                           al.Ethnicity = lml.Ethnicity and
                                           al.PG_AgeBandID = lml.PG_AgeBandID


                        GROUP BY
                        al.AgeBand,
                        al.AgeGroup,
                        lml.LR_MYSID,
                        al.Gender,
                        al.LLDD,
                        al.Ethnicity,
                        lml.EthnicityDetails,
                        lml.LDDetails,
                        lml.DisabilityDetails,
                        al.PG_AgeBandID,
                        lml.PG_ExpEndYrID,
                        lml.ExpEndYrNumber)

SELECT
*
FROM
(
SELECT
--AgeBand,
--PG_AgeBandID,
AgeGroup,
'Gender' as Stat_Type,
Gender as Grouping_Val,
'' AS Details,
sum(isnull(pvstart,0)) as starts,
sum(isnull(PVXfr,0)) as transfers,
sum(isnull(PVFund,0)) as LSC_funded,
sum(isnull(PVCont,0)) as continuing,
sum(isnull(PVComp,0)) as completed,
sum(isnull(PVAch,0)) as achieved,
sum(isnull(PVHigh,0)) as highgrades,
sum(isnull(exam,0)) as exam_taken_not_know,
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvstart,0))) AS 'Success Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVComp,0)) + sum(isnull(PVCont,0)) , sum(pvstart)) AS 'Retention Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvcomp,0))) AS 'Achievment Rate'

FROM
CoreProAchData

GROUP BY
--AgeBand,
--PG_AgeBandID,
AgeGroup,
Gender

UNION ALL SELECT
--AgeBand,
--PG_AgeBandID,
AgeGroup,
'LLDD Break Down' as Stat_Type,
'Learning Difficulty'  as Grouping_Val,
LDDetails AS Details, 
sum(isnull(pvstart,0)) as starts,
sum(isnull(PVXfr,0)) as transfers,
sum(isnull(PVFund,0)) as LSC_funded,
sum(isnull(PVCont,0)) as continuing,
sum(isnull(PVComp,0)) as completed,
sum(isnull(PVAch,0)) as achieved,
sum(isnull(PVHigh,0)) as highgrades,
sum(isnull(exam,0)) as exam_taken_not_know,
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvstart,0))) AS 'Success Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVComp,0)) + sum(isnull(PVCont,0)) , sum(pvstart)) AS 'Retention Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvcomp,0))) AS 'Achievment Rate'

FROM
CoreProAchData

WHERE
LDDetails != 'none' AND
LDDetails != 'not known/not provided'

GROUP BY
--AgeBand,
--PG_AgeBandID,
AgeGroup,
LDDetails

UNION ALL SELECT
--AgeBand,
--PG_AgeBandID,
AgeGroup,
'LLDD Break Down' as Stat_Type,
'No Learning Difficulty'  as Grouping_Val,
LDDetails AS Details, 
sum(isnull(pvstart,0)) as starts,
sum(isnull(PVXfr,0)) as transfers,
sum(isnull(PVFund,0)) as LSC_funded,
sum(isnull(PVCont,0)) as continuing,
sum(isnull(PVComp,0)) as completed,
sum(isnull(PVAch,0)) as achieved,
sum(isnull(PVHigh,0)) as highgrades,
sum(isnull(exam,0)) as exam_taken_not_know,
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvstart,0))) AS 'Success Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVComp,0)) + sum(isnull(PVCont,0)) , sum(pvstart)) AS 'Retention Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvcomp,0))) AS 'Achievment Rate'

FROM
CoreProAchData

WHERE
LDDetails = 'none'

GROUP BY
--AgeBand,
--PG_AgeBandID,
AgeGroup,
LDDetails

UNION ALL SELECT
--AgeBand,
--PG_AgeBandID,
AgeGroup,
'LLDD Break Down' as Stat_Type,
'Unknown'  as Grouping_Val,
LDDetails AS Details, 
sum(isnull(pvstart,0)) as starts,
sum(isnull(PVXfr,0)) as transfers,
sum(isnull(PVFund,0)) as LSC_funded,
sum(isnull(PVCont,0)) as continuing,
sum(isnull(PVComp,0)) as completed,
sum(isnull(PVAch,0)) as achieved,
sum(isnull(PVHigh,0)) as highgrades,
sum(isnull(exam,0)) as exam_taken_not_know,
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvstart,0))) AS 'Success Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVComp,0)) + sum(isnull(PVCont,0)) , sum(pvstart)) AS 'Retention Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvcomp,0))) AS 'Achievment Rate'

FROM
CoreProAchData

WHERE
LDDetails = 'not known/not provided'

GROUP BY
--AgeBand,
--PG_AgeBandID,
AgeGroup,
LDDetails

UNION ALL SELECT
--AgeBand,
--PG_AgeBandID,
AgeGroup,
'LLDD Break Down' as Stat_Type,
'Disability'  as Grouping_Val,
DisabilityDetails AS Details, 
sum(isnull(pvstart,0)) as starts,
sum(isnull(PVXfr,0)) as transfers,
sum(isnull(PVFund,0)) as LSC_funded,
sum(isnull(PVCont,0)) as continuing,
sum(isnull(PVComp,0)) as completed,
sum(isnull(PVAch,0)) as achieved,
sum(isnull(PVHigh,0)) as highgrades,
sum(isnull(exam,0)) as exam_taken_not_know,
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvstart,0))) AS 'Success Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVComp,0)) + sum(isnull(PVCont,0)) , sum(pvstart)) AS 'Retention Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvcomp,0))) AS 'Achievment Rate'

FROM
CoreProAchData

WHERE
DisabilityDetails != 'no disability' AND
DisabilityDetails != 'not known/not provided'

GROUP BY
--AgeBand,
--PG_AgeBandID,
AgeGroup,
DisabilityDetails

UNION ALL SELECT
--AgeBand,
--PG_AgeBandID,
AgeGroup,
'LLDD Break Down' as Stat_Type,
'No Disability'  as Grouping_Val,
DisabilityDetails AS Details, 
sum(isnull(pvstart,0)) as starts,
sum(isnull(PVXfr,0)) as transfers,
sum(isnull(PVFund,0)) as LSC_funded,
sum(isnull(PVCont,0)) as continuing,
sum(isnull(PVComp,0)) as completed,
sum(isnull(PVAch,0)) as achieved,
sum(isnull(PVHigh,0)) as highgrades,
sum(isnull(exam,0)) as exam_taken_not_know,
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvstart,0))) AS 'Success Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVComp,0)) + sum(isnull(PVCont,0)) , sum(pvstart)) AS 'Retention Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvcomp,0))) AS 'Achievment Rate'

FROM
CoreProAchData

WHERE
DisabilityDetails = 'no disability'

GROUP BY
--AgeBand,
--PG_AgeBandID,
AgeGroup,
DisabilityDetails

UNION ALL SELECT
--AgeBand,
--PG_AgeBandID,
AgeGroup,
'LLDD Break Down' as Stat_Type,
'Unknown' as Grouping_Val,
DisabilityDetails AS Details, 
sum(isnull(pvstart,0)) as starts,
sum(isnull(PVXfr,0)) as transfers,
sum(isnull(PVFund,0)) as LSC_funded,
sum(isnull(PVCont,0)) as continuing,
sum(isnull(PVComp,0)) as completed,
sum(isnull(PVAch,0)) as achieved,
sum(isnull(PVHigh,0)) as highgrades,
sum(isnull(exam,0)) as exam_taken_not_know,
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvstart,0))) AS 'Success Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVComp,0)) + sum(isnull(PVCont,0)) , sum(pvstart)) AS 'Retention Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvcomp,0))) AS 'Achievment Rate'

FROM
CoreProAchData

WHERE
DisabilityDetails = 'not known/not provided'

GROUP BY
--AgeBand,
--PG_AgeBandID,
AgeGroup,
DisabilityDetails

UNION ALL SELECT
--AgeBand,
--PG_AgeBandID,
AgeGroup,
'LLDD' as Stat_Type,
CASE WHEN LLDD ='NO_LLDD' THEN 'No ' ELSE '' END + 'Learning Disability / Difficulty'  as Grouping_Val,
'' AS Details,
sum(isnull(pvstart,0)) as starts,
sum(isnull(PVXfr,0)) as transfers,
sum(isnull(PVFund,0)) as LSC_funded,
sum(isnull(PVCont,0)) as continuing,
sum(isnull(PVComp,0)) as completed,
sum(isnull(PVAch,0)) as achieved,
sum(isnull(PVHigh,0)) as highgrades,
sum(isnull(exam,0)) as exam_taken_not_know,
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvstart,0))) AS 'Success Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVComp,0)) + sum(isnull(PVCont,0)) , sum(pvstart)) AS 'Retention Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvcomp,0))) AS 'Achievment Rate'

FROM
CoreProAchData

GROUP BY
--AgeBand,
--PG_AgeBandID,
AgeGroup,
LLDD

UNION ALL SELECT
--AgeBand,
--PG_AgeBandID,
AgeGroup,
'Ethnicity' as Stat_Type,
CASE WHEN Ethnicity ='NWB' THEN 'Non ' ELSE '' END + 'White British' as Grouping_Val,
EthnicityDetails AS Details,
sum(isnull(pvstart,0)) as starts,
sum(isnull(PVXfr,0)) as transfers,
sum(isnull(PVFund,0)) as LSC_funded,
sum(isnull(PVCont,0)) as continuing,
sum(isnull(PVComp,0)) as completed,
sum(isnull(PVAch,0)) as achieved,
sum(isnull(PVHigh,0)) as highgrades,
sum(isnull(exam,0)) as exam_taken_not_know,
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvstart,0))) AS 'Success Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVComp,0)) + sum(isnull(PVCont,0)) , sum(pvstart)) AS 'Retention Rate',
ReportingFramework.dbo.fsCIS_CalculatePercentages(sum(isnull(PVAch,0)) , sum(isnull(pvcomp,0))) AS 'Achievment Rate'

FROM
CoreProAchData

GROUP BY
--AgeBand,
--PG_AgeBandID,
AgeGroup,
Ethnicity,
EthnicityDetails) as sub

ORDER BY
--convert(int,PG_AgeBandID),
Stat_Type,
Grouping_Val

2 个答案:

答案 0 :(得分:3)

这是一个猜测,但评论太久了。在获取长流错误消息时,查看第一个错误消息通常很有帮助:

Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Expr2522'.

嗯,这很奇怪,因为查询中没有名为expr2522的列。这看起来也像SQL编译器将包含在执行计划中。

我怀疑数据库中发生了一些变化。我的建议是重新编译所有查询,存储过程,函数和视图。重新编译将失败并为您提供更易理解的错误消息。或者,它可能会将查询和视图调整为新的数据库结构,以便它们可以正常工作。

答案 1 :(得分:1)

查询是否引用数据库中的任何视图?如果它们不是使用模式绑定创建的,那么底层数据库模式可能会发生更改,从而使视图无效。

ReportingFramework.dbo.fsCIS_CalculatePercentages函数也可能已更改。

简短的故事是,最可能的原因是数据库架构或相关功能或过程的变化。您需要将查询拆开并一次测试一个部分,以尝试识别导致其失败的部分。

我会从

开始
SELECT TOP 100 * FROM CoreProAchData

看看是否有效,然后从那里建立起来。