<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 中的子句

        时间:2023-07-17
        <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 中的子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQL Server 索引 - 升序或降序,有什么区别? 下一篇:无法打开数据库,因为它是版本 782.此服务器支持版本 706 及更早版本.不支持降级路径

                  相关文章

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

                    1. <tfoot id='SFio8'></tfoot>

                    2. <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'>