<bdo id='Ykde1'></bdo><ul id='Ykde1'></ul>

        <small id='Ykde1'></small><noframes id='Ykde1'>

        <i id='Ykde1'><tr id='Ykde1'><dt id='Ykde1'><q id='Ykde1'><span id='Ykde1'><b id='Ykde1'><form id='Ykde1'><ins id='Ykde1'></ins><ul id='Ykde1'></ul><sub id='Ykde1'></sub></form><legend id='Ykde1'></legend><bdo id='Ykde1'><pre id='Ykde1'><center id='Ykde1'></center></pre></bdo></b><th id='Ykde1'></th></span></q></dt></tr></i><div id='Ykde1'><tfoot id='Ykde1'></tfoot><dl id='Ykde1'><fieldset id='Ykde1'></fieldset></dl></div>
        <legend id='Ykde1'><style id='Ykde1'><dir id='Ykde1'><q id='Ykde1'></q></dir></style></legend>
      1. <tfoot id='Ykde1'></tfoot>

        “案例"“WHERE"中的语句SQL Server 2008 中的子句

        quot;CASEquot; statement within quot;WHEREquot; clause in SQL Server 2008(“案例“WHERE中的语句SQL Server 2008 中的子句)
        <legend id='ydBSo'><style id='ydBSo'><dir id='ydBSo'><q id='ydBSo'></q></dir></style></legend>

              • <bdo id='ydBSo'></bdo><ul id='ydBSo'></ul>
              • <small id='ydBSo'></small><noframes id='ydBSo'>

                    <tbody id='ydBSo'></tbody>
                  <i id='ydBSo'><tr id='ydBSo'><dt id='ydBSo'><q id='ydBSo'><span id='ydBSo'><b id='ydBSo'><form id='ydBSo'><ins id='ydBSo'></ins><ul id='ydBSo'></ul><sub id='ydBSo'></sub></form><legend id='ydBSo'></legend><bdo id='ydBSo'><pre id='ydBSo'><center id='ydBSo'></center></pre></bdo></b><th id='ydBSo'></th></span></q></dt></tr></i><div id='ydBSo'><tfoot id='ydBSo'></tfoot><dl id='ydBSo'><fieldset id='ydBSo'></fieldset></dl></div>

                • <tfoot id='ydBSo'></tfoot>
                • 本文介绍了“案例"“WHERE"中的语句SQL Server 2008 中的子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在处理一个在WHERE"子句中包含CASE"语句的查询.但是 SQL Server 2008 在执行时出现了一些错误.任何人都可以帮助我进行正确的查询吗?这是查询:

                  I am working with a query which contains "CASE" statement within "WHERE" clause. But SQL Server 2008 is giving some errors while executing it. Can anyone please help me with the correct query? Here is the query:

                  SELECT
                      tl.storenum 'Store #', 
                      co.ccnum 'FuelFirst Card #', 
                      co.dtentered 'Date Entered',
                      CASE st.reasonid 
                          WHEN 1 THEN 'Active' 
                     WHEN 2 THEN 'Not Active' 
                     WHEN 0 THEN st.ccstatustypename 
                     ELSE 'Unknown' 
                      END 'Status',
                      CASE st.ccstatustypename 
                          WHEN 'Active' THEN ' ' 
                     WHEN 'Not Active' THEN ' ' 
                     ELSE st.ccstatustypename 
                      END 'Reason',
                      UPPER(REPLACE(REPLACE(co.personentered,'RT\\',''),'RACETRAC\\','')) 'Person Entered',
                      co.comments 'Comments or Notes'
                  FROM 
                      comments co
                      INNER JOIN cards cc ON co.ccnum=cc.ccnum
                      INNER JOIN customerinfo ci ON cc.customerinfoid=ci.customerinfoid
                      INNER JOIN ccstatustype st ON st.ccstatustypeid=cc.ccstatustypeid
                      INNER JOIN customerstatus cs ON cs.customerstatuscd=ci.customerstatuscd
                      INNER JOIN transactionlog tl ON tl.transactionlogid=co.transactionlogid
                      LEFT JOIN stores s ON s.StoreNum = tl.StoreNum
                  WHERE 
                      CASE LEN('TestPerson')
                          WHEN 0 THEN co.personentered  = co.personentered
                     ELSE co.personentered LIKE '%TestPerson'
                      END 
                      AND cc.ccnum = CASE LEN('TestFFNum')
                          WHEN 0 THEN cc.ccnum 
                     ELSE 'TestFFNum' 
                      END 
                      AND CASE LEN('2011-01-09 11:56:29.327') 
                          WHEN 0 THEN co.DTEntered = co.DTEntered 
                     ELSE 
                         CASE LEN('2012-01-09 11:56:29.327') 
                             WHEN 0 THEN co.DTEntered >= '2011-01-09 11:56:29.327' 
                        ELSE co.DTEntered BETWEEN '2011-01-09 11:56:29.327' AND '2012-01-09 11:56:29.327' 
                         END 
                      END
                      AND tl.storenum < 699 
                  ORDER BY tl.StoreNum
                  

                  推荐答案

                  首先,CASE 语句必须是表达式的一部分,而不是表达式本身.

                  First off, the CASE statement must be part of the expression, not the expression itself.

                  换句话说,你可以:

                  WHERE co.DTEntered = CASE 
                                            WHEN LEN('blah') = 0 
                                                 THEN co.DTEntered 
                                            ELSE '2011-01-01' 
                                       END 
                  

                  但它不会像你写的那样工作,例如:

                  But it won't work the way you have written them eg:

                  WHERE 
                      CASE LEN('TestPerson')
                          WHEN 0 THEN co.personentered  = co.personentered
                     ELSE co.personentered LIKE '%TestPerson'
                      END 
                  

                  使用这样的组合 OR 语句可能会更好:

                  You may have better luck using combined OR statements like this:

                  WHERE (
                          (LEN('TestPerson') = 0 
                               AND co.personentered = co.personentered
                          ) 
                          OR 
                          (LEN('TestPerson') <> 0 
                               AND co.personentered LIKE '%TestPerson')
                        )
                  

                  虽然,无论哪种方式,我都不确定您会获得多棒的查询计划.WHERE 子句中的这些类型的恶作剧通常会阻止查询优化器使用索引.

                  Although, either way I'm not sure how great of a query plan you'll get. These types of shenanigans in a WHERE clause will often prevent the query optimizer from utilizing indexes.

                  这篇关于“案例"“WHERE"中的语句SQL Server 2008 中的子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)

                  <legend id='SFio8'><style id='SFio8'><dir id='SFio8'><q id='SFio8'></q></dir></style></legend>
                    <bdo id='SFio8'></bdo><ul id='SFio8'></ul>

                          <tbody id='SFio8'></tbody>
                        • <tfoot id='SFio8'></tfoot>

                          • <i id='SFio8'><tr id='SFio8'><dt id='SFio8'><q id='SFio8'><span id='SFio8'><b id='SFio8'><form id='SFio8'><ins id='SFio8'></ins><ul id='SFio8'></ul><sub id='SFio8'></sub></form><legend id='SFio8'></legend><bdo id='SFio8'><pre id='SFio8'><center id='SFio8'></center></pre></bdo></b><th id='SFio8'></th></span></q></dt></tr></i><div id='SFio8'><tfoot id='SFio8'></tfoot><dl id='SFio8'><fieldset id='SFio8'></fieldset></dl></div>

                            <small id='SFio8'></small><noframes id='SFio8'>