If语句的语法错误

时间:2015-06-22 16:25:42

标签: sql syntax

我正在尝试创建一个If语句,如果项目超过100K或公司名为CGELENA或CGFEDUN,将查找并列出以下值,如果没有,则检查商品类型是否列在下面

If (ord_miscqty >='100000' OR m.cmp_id in('CGELENA','CGFEDUN')
    Begin
    Select 
    ord_number
        ,ord_status
        ,m.cmp_name
        ,Ord_startdate
        ,ord_miscqty AS [FreightValue]
        ,c.car_ins_cargolimits AS [CargoAmount]
        ,o.ord_carrier AS [Carrier]
        ,l.NAME AS [LSM2]
        ,c.car_iccnum AS [MCNumber]
        ,o.ord_description AS [Commodity]
        ,f.cmd_code [Commody Code]
        ,(
            SELECT t.ta_quantity
            FROM trlaccessories AS t
            WHERE o.ord_carrier = t.ta_trailer
                AND t.ta_type = 'POWER'
            ) AS [PowerUnits]
        ,(
            SELECT n.not_text
            FROM notes AS n
            WHERE n.nre_tablekey = o.ord_carrier
                AND n.not_type = 'I'
            ) AS [Note]
        ,(
            SELECT n.last_updatedatetime
            FROM notes AS n
            WHERE n.nre_tablekey = o.ord_carrier
                AND n.not_type = 'I'
            ) AS [NoteDate]
        ,(
            SELECT q.caq_type
            FROM carrierqualifications AS q
            WHERE o.ord_carrier = q.caq_id
                AND q.caq_type = 'RES'
            ) AS [Restriction]
        ,(
            SELECT n.not_expires
            FROM notes AS n
            WHERE n.nre_tablekey = o.ord_carrier
                AND n.not_type = 'I'
            ) AS [NoteExp]
    FROM orderheader AS o
    INNER JOIN carrier AS c ON o.ord_carrier = c.car_id
    INNER JOIN company AS m ON o.ord_billto = m.cmp_id
    INNER JOIN labelfile AS l ON o.ord_revtype3 = l.abbr
    join stops as s on s.ord_hdrnumber = o.ord_hdrnumber
    join FREIGHTDETAIL as f on f.stp_number = s.stp_number
    Where datediff(hour, getdate(), ord_startdate) > 0
        AND ord_status IN (
            'PLN'
            ,'DSP'
            )
        AND l.labeldefinition = 'RevType3'
    End 

    Else If
    f.cmd_code in ('COPSUL',
    'HAZMOLY',
    'COPANO',
    'COPCAN',
    'COPING',
    'COPPAN',
    'COPPER',
    'COPROD',
    'COPSTSHT',
    'COPSULNH',
    'APPLINCS',
    'LIQUOR') 
    Begin
    Select 
    ord_number
        ,ord_status
        ,m.cmp_name
        ,Ord_startdate
        ,ord_miscqty AS [FreightValue]
        ,c.car_ins_cargolimits AS [CargoAmount]
        ,o.ord_carrier AS [Carrier]
        ,l.NAME AS [LSM2]
        ,c.car_iccnum AS [MCNumber]
        ,o.ord_description AS [Commodity]
        ,f.cmd_code [Commody Code]
        ,(
            SELECT t.ta_quantity
            FROM trlaccessories AS t
            WHERE o.ord_carrier = t.ta_trailer
                AND t.ta_type = 'POWER'
            ) AS [PowerUnits]
        ,(
            SELECT n.not_text
            FROM notes AS n
            WHERE n.nre_tablekey = o.ord_carrier
                AND n.not_type = 'I'
            ) AS [Note]
        ,(
            SELECT n.last_updatedatetime
            FROM notes AS n
            WHERE n.nre_tablekey = o.ord_carrier
                AND n.not_type = 'I'
            ) AS [NoteDate]
        ,(
            SELECT q.caq_type
            FROM carrierqualifications AS q
            WHERE o.ord_carrier = q.caq_id
                AND q.caq_type = 'RES'
            ) AS [Restriction]
        ,(
            SELECT n.not_expires
            FROM notes AS n
            WHERE n.nre_tablekey = o.ord_carrier
                AND n.not_type = 'I'
            ) AS [NoteExp]
    FROM orderheader AS o
    INNER JOIN carrier AS c ON o.ord_carrier = c.car_id
    INNER JOIN company AS m ON o.ord_billto = m.cmp_id
    INNER JOIN labelfile AS l ON o.ord_revtype3 = l.abbr
    join stops as s on s.ord_hdrnumber = o.ord_hdrnumber
    join FREIGHTDETAIL as f on f.stp_number = s.stp_number
    Where datediff(hour, getdate(), ord_startdate) > 0
        AND ord_status IN (
            'PLN'
            ,'DSP'
            )
        AND l.labeldefinition = 'RevType3'
    End 

1 个答案:

答案 0 :(得分:0)

在else语句中,您必须使用 SELECT

Else If
Select f.cmd_code in ('COPSUL',...
....