SQL Server-无法准备语句-CASE问题

时间:2019-03-07 13:31:37

标签: sql-server-2016

我正在处理查询。一切进展顺利,直到我碰到这个障碍,但我仍无法弄清楚问题出在哪里。

我不应该只能访问Report Builder 3.0中的查询生成器。在我的组织中,除非您是IT人员,否则您将无法访问SQL Server Management Studio。...不要让我入门。

这是查询:

    SELECT
    [SUM].[REFNO]
    ,[SUM].[CURR-APPLICATION]
    ,[SUM].[RN]
    ,[SUM].[Med_Process_Start]
    ,[SUM].[SENT]
    ,[SUM].[RCVD]
    ,[SUM].[MD-DS]
    ,[SUM].[MD-RA]
    ,[SUM].[MN-DS]
    ,[SUM].[MN-RA]
    ,(CASE
    /* ---------------------------------------------------------------------- */
        WHEN
            [SUM].[MD-RA] IS NULL AND
            [SUM].[MN-RA] IS NULL
        THEN
            CASE
                WHEN
                    [SUM].[MD-DS] IS NULL AND
                    [SUM].[MN-DS] IS NULL
                THEN
                    CASE
                        WHEN
                            [SUM].[SENT] IS NOT NULL AND
                            [SUM].[RCVD] IS NULL
                        THEN    'Issued'                            
                        WHEN
                            [SUM].[RCVD] IS NOT NULL
                        THEN    'Waiting for decision'
                    END
                WHEN
                    [SUM].[MD-DS] IS NOT NULL AND
                    [SUM].[MN-DS] IS NULL
                THEN
                    CASE
                        WHEN    
                            [SUM].[RCVD] IS NULL
                        THEN    '(e) Missing received Date'
                        WHEN
                            [SUM].[RCVD] IS NOT NULL AND
                            [SUM].[RCVD] <= [SUM].[MD-DS]
                        THEN    'Decision Medical'
                        WHEN
                            [SUM].[RCVD] IS NOT NULL AND
                            [SUM].[RCVD] > [SUM].[MD-DS]
                        THEN    'Waiting for NEW decision'
                    END
                WHEN
                    [SUM].[MD-DS] IS NULL AND
                    [SUM].[MN-DS] IS NOT NULL
                THEN
                    CASE
                        WHEN    
                            [SUM].[RCVD] IS NULL
                        THEN    '(e) Missing received Date'
                        WHEN
                            [SUM].[RCVD] IS NOT NULL
                        THEN
                            CASE
                                WHEN
                                    [SUM].[RCVD] <= [SUM].[MN-DS]
                                THEN    'Decision Mental'
                                WHEN
                                    [SUM].[RCVD] > [SUM].[MN-DS]
                                THEN    'Waiting for NEW decision'
                            END
                    END
                WHEN
                    [SUM].[MD-DS] IS NOT NULL AND
                    [SUM].[MN-DS] IS NOT NULL
                THEN
                    CASE
                        WHEN    
                            [SUM].[RCVD] IS NULL
                        THEN    '(e) Missing received Date'
                        WHEN
                            [SUM].[RCVD] IS NOT NULL
                        THEN
                            CASE
                                WHEN
                                    [SUM].[RCVD] <= [SUM].[MD-DS] AND
                                    [SUM].[RCVD] <= [SUM].[MN-DS]
                                THEN    'Decision Both'
                                WHEN
                                    [SUM].[RCVD] > [SUM].[MD-DS] AND 
                                    [SUM].[RCVD] > [SUM].[MN-DS]
                                THEN    'Waiting for NEW decision'
                                WHEN
                                    [SUM].[RCVD] > [SUM].[MD-DS] AND 
                                    [SUM].[RCVD] <= [SUM].[MN-DS]
                                THEN    '(c) Decision Mental'
                                WHEN
                                    [SUM].[RCVD] <= [SUM].[MD-DS] AND 
                                    [SUM].[RCVD] > [SUM].[MN-DS]
                                THEN    '(c) Decision Medical'
                            END /*[LEVEL2.1-4]*/    
                    END /*[LEVEL2.1-3]*/
            END /*[LEVEL2.1]*/
    /* ---------------------------------------------------------------------- */
        WHEN
            [SUM].[MD-RA] IS NOT NULL AND
            [SUM].[MN-RA] IS NULL
        THEN
            CASE
                WHEN 
                    [SUM].[MD-DS] IS NULL
                THEN    '(e) MD re-assess without assess'
                WHEN 
                    [SUM].[MD-RA] < [SUM].[MD-DS]
                THEN    '(e) MD re-assess before assess'
                WHEN 
                    [SUM].[MD-RA] >= [SUM].[MD-DS]
                THEN    '(c) Re-assessment Decision Medical'
            END /*[LEVEL2.2]*/
    /* ---------------------------------------------------------------------- */
        WHEN
            [SUM].[MD-RA] IS NULL AND
            [SUM].[MN-RA] IS NOT NULL
        THEN
            CASE
                WHEN 
                    [SUM].[MN-DS] IS NULL
                THEN    '(e) MN re-assess without assess'
                WHEN 
                    [SUM].[MN-RA] < [SUM].[MN-DS]
                THEN    '(e) MN re-assess before assess'
                WHEN 
                    [SUM].[MN-RA] >= [SUM].[MN-DS]
                THEN    '(c) Re-assessment Decision Mental'
            END /*[LEVEL2.3]*/
    /* ---------------------------------------------------------------------- */
        WHEN
            [SUM].[MD-RA] IS NOT NULL AND
            [SUM].[MN-RA] IS NOT NULL
        THEN
            CASE
                WHEN    
                    [SUM].[RCVD] IS NULL
                THEN    '(e) Missing received Date'
                WHEN
                    [SUM].[RCVD] IS NOT NULL
                THEN
                    CASE
                        WHEN
                            [SUM].[RCVD] <= [SUM].[MD-RA] AND
                            [SUM].[RCVD] <= [SUM].[MN-RA]
                        THEN    'Re-assessment Decision Both'
                        WHEN
                            [SUM].[RCVD] > [SUM].[MD-RA] AND 
                            [SUM].[RCVD] > [SUM].[MN-RA]
                        THEN    'Waiting for NEW Re-assessment decision'
                        WHEN
                            [SUM].[RCVD] > [SUM].[MD-RA] AND 
                            [SUM].[RCVD] <= [SUM].[MN-RA]
                        THEN    '(c) Re-assessment Decision Mental'
                        WHEN
                            [SUM].[RCVD] <= [SUM].[MD-RA] AND 
                            [SUM].[RCVD] > [SUM].[MN-RA]
                        THEN    '(c) Re-assessment Decision Medical'
                    END /*[LEVEL2.3-3]*/    
            END /*[LEVEL2.3]*/
    /* ---------------------------------------------------------------------- */
    ELSE 'Unknown'
    END) [MyCategory]
    ,(CASE
        WHEN 'a' is null then 'b'
        WHEN 'a' is not null then 'b'
    end) [greduygdg]
    ,(CASE
        WHEN 1 = 2 then 'b'
        WHEN 2 = 1 then 'b'
        else 'c'
    end) [grefeewfrewf]
  FROM
  [SUMMARY] [SUM]
  WHERE 
      [SUM].[Med_Process_Start] = 1
      AND [SUM].[CURR-APPLICATION] = 1

现在问题出在[MyCategory]CASE周围。我已经走了这么远,并且避免了可笑的复杂和视觉上可怕的代码,我想将其隔离为CTE,然后访问其中的列以做其他事情。

但是,如果我尝试访问[MyCategory]列将其包装为CTE甚至嵌套为子查询,则查询将失败,但并非总是失败。如果我SELECT *的所有列都经过甚至超过SELECT *,然后使用WHERE子句,我可以限制基于该列返回的行。

但是,如果我选择SELECT [MyCategory],则会得到以下信息:

===================================

An error occurred while reading data from the query result set.
Statement(s) could not be prepared. (Microsoft SQL Server Report Builder)

===================================

Statement(s) could not be prepared. (.Net SqlClient Data Provider)

------------------------------
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=13.00.5264&EvtSrc=MSSQLServer&EvtID=8180&LinkId=20476

------------------------------
Server Name: ***********
Error Number: 8180
Severity: 16
State: 1
Line Number: 1


------------------------------
Program Location:

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.HasMoreRows()
   at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
   at System.Data.SqlClient.SqlDataReader.Read()
   at Microsoft.ReportingServices.DataExtensions.DataReaderWrapper.Read()
   at Microsoft.ReportingServices.QueryDesigners.QueryResultsDlgStorage.EnsureRowsInBuf(Int64 FirstRowIndex, Int64 LastRowIndex)

如果我使用*但直接访问失败,对我来说查询如何包含列将对我毫无意义?

我不要求重写代码,但我看不出如何解决此问题。我尝试过使用不同的别名,以防发生冲突。不同的列名;紧接着,我添加了一些其他case语句(简单),并能够很好地访问它们。

这非常令人沮丧,您能给我的任何指教将不胜感激。

谢谢

0 个答案:

没有答案