T-SQL案例陈述混淆

时间:2012-01-04 16:26:50

标签: sql tsql

这是我正在清理的遗留查询,我对此案例陈述感到困惑,所以我们将非常感谢任何帮助。

SELECT CASE
  WHEN bitdelivered = 1 THEN
    '' + CONVERT(CHAR(10), deliv.dtmdeliverydate, 101)
    + '                '
    + 'þ' + '
' -- this is a carriage return, do not remove it
  WHEN bitdelivered = 0 AND deliv.dtmdeliverydate < Getdate() THEN
    '' + CONVERT(CHAR(10), deliv.dtmdeliverydate, 101)
    + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    + '&#168;' + '
'
  ELSE
    '' + CONVERT(CHAR(10), deliv.dtmdeliverydate, 101)
    + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    + '&#168;' + '
'
END

就像我说它非常脏,但是当我运行整个查询时,如果我为bitdelivered得到0,则casestatement正常工作,但是如果我得到1返回,那么我返回null而不是其他块。有什么想法吗?

编辑我已编辑了如下所示的代码,以便于阅读,我仍然无效...

select
CONVERT(CHAR(10), deliv.dtmdeliverydate, 101) +
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
CASE 
    WHEN bitdelivered = 1 THEN '&#254;'
    ELSE '&#168;'
END + '
'

编辑x 2 以下是整个(我非常讨厌的)sql查询,我没有写这个我只是想清理它...

SELECT dbo.tblrequirements.guidrequirementid,
       tblcontracting.strdescription                             AS
       strcontracting,
       dbo.viewawardnumbers.strcontractnumber                    AS
       strawardnumber,
       Ltrim(Rtrim(dbo.viewawardnumbers.strtonumber))            AS strtonumber,
       dbo.tblrequirements.strdescription,
       dbo.tblezquerycontractvalue.curtotalvalue,
       dbo.tblcodescontractvehicles.strdescription               AS
       strcontractvehicle,
       tblcustomer.stracronym                                    AS strcustomer,
       dbo.tblcodesrequirementstatuses.strdescription            AS strstatus,
       Substring(dbo.tblrequirements.strnotes, 0, 512)           AS strnotes,
       Coalesce (dbo.tblrequirements.guidfromid,
       '00000000-0000-0000-0000-000000000000'
       )                                                         AS guidfromid,
       Coalesce (dbo.viewteammembers.guidpersonid,
       '00000000-0000-0000-0000-000000000000')                   AS guidpersonid
       ,
       dbo.viewcurrentoptions.dtmstart,
       dbo.viewcurrentoptions.dtmend,
       Dateadd(d, -60, dbo.viewcurrentoptions.dtmend)            AS
       dtm1stnoticedue,
       Dateadd(d, -30, dbo.viewcurrentoptions.dtmend)            AS
       dtm2ndnoticedue,
       dbo.tblcontractdates.dtmcontractstart,
       dbo.tblcontractdates.dtmcontractend,
       Isnull(dbo.viewteammembers.strshortname, ' Not Assigned') AS strshortname
       ,
       dbo.tblezqueryfunding.curtotalfunded,
       CASE
         WHEN ( dbo.tblcodesrequirementstatuses.strdescription =
                'Pre-Solicitation'
                 OR dbo.tblcodesrequirementstatuses.strdescription =
                    'Solicitation'
                 OR dbo.tblcodesrequirementstatuses.strdescription =
                    'Source Selection'
              ) THEN 'P'
         WHEN ( Coalesce (dbo.tblcodesprocurementtypes.strcode, '') <> 'IT'
                AND Coalesce (dbo.tblcodesprocurementtypes.strcode, '') <> 'OPS'
              ) THEN
         'S'
         ELSE 'C'
       END                                                       AS strproctype,
       dbo.tblcodesprocurementtypes.strcode,
       dbo.tblcodesprocurementtypes.strdescription               AS strprocdesc,
       deliveries.dtmdeliverydate,
       deliveries.bitdelivered,
       CASE
         WHEN ( dbo.tblcodesrequirementstatuses.strdescription =
                'Pre-Solicitation'
                 OR dbo.tblcodesrequirementstatuses.strdescription =
                    'Solicitation'
                 OR dbo.tblcodesrequirementstatuses.strdescription =
                    'Source Selection'
              ) THEN 'Initial Start Date'
         WHEN ( Coalesce (dbo.tblcodesprocurementtypes.strcode, '') <> 'IT'
                AND Coalesce (dbo.tblcodesprocurementtypes.strcode, '') <> 'OPS'
              ) THEN
         'POP'
         ELSE 'Delivery Date'
       END                                                       AS
       colpopdelivdt,
       CASE
         WHEN ( dbo.tblcodesrequirementstatuses.strdescription =
                'Pre-Solicitation'
                 OR dbo.tblcodesrequirementstatuses.strdescription =
                    'Solicitation'
                 OR dbo.tblcodesrequirementstatuses.strdescription =
                    'Source Selection'
              ) THEN 'Req Award Date'
         WHEN ( Coalesce (dbo.tblcodesprocurementtypes.strcode, '') <> 'IT'
                AND Coalesce (dbo.tblcodesprocurementtypes.strcode, '') <> 'OPS'
              ) THEN
         'Option Notice'
         ELSE 'Delivered'
       END                                                       AS
       coloptnotdeliv,
       tblpresolicitations.dtmcontacted,
       tblpresolicitations.dtmrequiredby,
       tblpresolicitations.dtmawardnotice,
       strjobid,
       bitprimary
FROM   dbo.tblrequirements
       INNER JOIN dbo.tblcodesrequirementstatuses
         ON dbo.tblcodesrequirementstatuses.strcode =
            dbo.tblrequirements.strstatusid
       LEFT OUTER JOIN dbo.tblezquerycontractvalue
         ON dbo.tblezquerycontractvalue.guidrequirementid =
            dbo.tblrequirements.guidrequirementid
       LEFT OUTER JOIN dbo.viewawardnumbers
         ON dbo.viewawardnumbers.guidrequirementid =
            dbo.tblrequirements.guidrequirementid
       LEFT OUTER JOIN dbo.tblezqueryfunding
         ON dbo.tblezqueryfunding.strawardnumber =
            dbo.viewawardnumbers.strawardnumber
       LEFT OUTER JOIN dbo.viewcurrentoptions
         ON dbo.tblrequirements.guidrequirementid =
            dbo.viewcurrentoptions.guidrequirementid
       LEFT OUTER JOIN dbo.viewteammembers
         ON dbo.viewteammembers.guidrequirementid =
            dbo.tblrequirements.guidrequirementid
       LEFT OUTER JOIN dbo.tblcontracts
         ON dbo.tblcontracts.guidrequirementid =
            dbo.tblrequirements.guidrequirementid
       LEFT OUTER JOIN dbo.tblcontractdates
         ON dbo.tblcontractdates.guidrequirementid =
            dbo.tblrequirements.guidrequirementid
       LEFT OUTER JOIN dbo.tblcodescontractvehicles
         ON dbo.tblcodescontractvehicles.strcode = dbo.tblcontracts.strvehicleid
       LEFT OUTER JOIN dbo.tblorganizations AS tblcontracting
         ON dbo.tblrequirements.guidfromid = tblcontracting.guidorgid
       LEFT OUTER JOIN dbo.tblorganizations AS tblcustomer
         ON dbo.tblrequirements.guidtoid = tblcustomer.guidorgid
       LEFT OUTER JOIN dbo.tblpresolicitations
         ON dbo.tblpresolicitations.guidrequirementid =
            dbo.tblrequirements.guidrequirementid
       LEFT OUTER JOIN dbo.tblcodesprocurementtypes
         ON dbo.tblcodesprocurementtypes.strcode =
            dbo.tblpresolicitations.strprocurementtypeid
       LEFT OUTER JOIN (SELECT guidrequirementid,
                               REPLACE(REPLACE(REPLACE (
(select
ISNULL(CONVERT(CHAR(10), deliv.dtmdeliverydate, 101), '(no date)') +
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
CASE 
    WHEN bitdelivered = 1 THEN '&#254;'
    ELSE '&#168;'
END + '
'



                                       /*(SELECT CASE
                                                 WHEN bitdelivered = 1
                                               THEN
                                                 ''
                                                 +
                                                 CONVERT(CHAR(10),
       deliv.dtmdeliverydate,
       101) +
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
+ '&#254;' + '

'
WHEN bitdelivered = 0
AND deliv.dtmdeliverydate < Getdate() THEN
'' +
CONVERT(CHAR(10), deliv.dtmdeliverydate, 101) +
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
+ '&#168;' + '

'
ELSE 
'' + CONVERT(CHAR(10), deliv.dtmdeliverydate, 101) +
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
+ '&#168;' + '

'
END*/
FROM   tblclins clins
INNER JOIN tblcommodities commod
ON commod.guidclinid = clins.guidclinid
INNER JOIN tbldeliveries deliv
ON deliv.guidcommodityid = commod.guidcommodityid
WHERE  clins.guidrequirementid = req.guidrequirementid
AND deliv.bitdelivered = 0
ORDER  BY guidrequirementid,
deliv.dtmdeliverydate
FOR XML PATH('')), '&lt;', '<'), '&gt;', '>'), '&amp;', '&') AS dtmdeliverydate
,
Stuff ((SELECT '|' + CAST(bitdelivered AS CHAR(1))
FROM   tblclins clins
INNER JOIN tblcommodities commod
ON commod.guidclinid = clins.guidclinid
INNER JOIN tbldeliveries deliv
ON deliv.guidcommodityid = commod.guidcommodityid
WHERE  clins.guidrequirementid = req.guidrequirementid
ORDER  BY guidrequirementid
FOR XML PATH('')), 1, 1, '')                          AS bitdelivered
FROM   tblrequirements req
GROUP  BY guidrequirementid) deliveries
  ON deliveries.guidrequirementid = dbo.tblrequirements.guidrequirementid

4 个答案:

答案 0 :(得分:2)

CASE声明的条款是这个

WHEN bitdelivered = 1

所以它永远不会进入你似乎认为应该通过这个语句的ELSE语句“但是如果我得到1返回,那么我返回一个null而不是else块”

如果您从此案例陈述中获取空值,因为deliv.dtmdeliverydateNULLCONCAT_NULL_YIELDS_NULL设为ON

您应该使用COALESCEISNULL来解决此问题。您可以将设置CONCAT_NULL_YIELDS_NULL更改为关闭,但我不建议这样做,因为此设置让人感到惊讶。

然而,正如我们在our discussion期间在派生表上的WHERE子句中找到的那样

WHERE clins.guidrequirementid = req.guidrequirementid AND deliv.bitdelivered = 0表示

CASE WHEN bitdelivered = 1

永远不会被评估。

答案 1 :(得分:1)

在这种情况下,deliv.dtmdeliverydate可能是NULL

如果是,则CONVERT(CHAR(10), deliv.dtmdeliverydate, 101)将产生NULL并将其连接到字符串将产生NULL

答案 2 :(得分:1)

您可以将其简化为一个。我可能错过了一些回车

ISNULL(CONVERT(CHAR(10), deliv.dtmdeliverydate, 101), '(no date)') +
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
CASE 
    WHEN bitdelivered = 1 THEN '&#254;'
    ELSE '&#168;'
END + '
'

答案 3 :(得分:0)

如果当你的bitdelivered为1时deliv.dtmdeliverydate为null,那么结果值将为null ..