关键字 if 附近的语法不正确

问题描述 投票:0回答:1

这可能很简单,但我没有看到。我收到以下 SQL 的错误消息

Incorrect syntax near the keyword 'if'

DF.IsBillingEntity
是一个位列。

select OBS_CC.OBSVALUE                      'Cost Center'
      ,U.LASTNAME                           'Staff Lastname'
      ,U.FIRSTNAME                          'Staff Firstname'
      ,U.LOGINNAME                          'Staff Login'

      ,if (DF.IsBillingEntity = 1)
         select 'Billable';
       else
         select 'Not Billable';             

      ,DF.Suffix                            'Credentials'
      ,OBS_SUP.OBSVALUE                     'Supervisor'
      ,D.SUMMARY                            'Note Name'
      ,D.DB_CREATE_DATE                     'Date Created'
      ,dbo.Convert_ID_to_date(D.PUBTIME)    'Note Signed'
   
from DOCUMENT D 
     left join OBS OBS_CC on OBS_CC.SDID = D.SDID and OBS_CC.HDID=75246
     left join PatientProfile P on D.SDID = P.SSDID
     left join OBS OBS_SUP on OBS_SUP.SDID=D.SDID and OBS_SUP.HDID=711437
     inner join USR U on D.USRID=U.PVID
     left join DoctorFacility DF on U.PVID=DF.PVId
where OBS_SUP.OBSVALUE is not null
      and dbo.cusExcludeRecords(P.PID,U.PVId) = -1
order by 1,2,3,6
sql
1个回答
0
投票

我认为您需要 CASE(或某些 RDBMS 的 IIF)而不是 IF。 IF用于流量控制,不能用作列表达式。请参阅如何在 SQL SELECT 中执行 IF...THEN?

© www.soinside.com 2019 - 2024. All rights reserved.