语法错误

时间:2009-02-12 20:32:54

标签: sql

下面的陈述有什么问题?我一直收到以下错误消息.... 服务器:消息156,级别15,状态1,行4 关键字'THEN'附近的语法不正确。

update oildatasetstatus
set oildatasetstatusid = 
    case 
    WHEN 5 THEN 16        
    WHEN 6 THEN 17        
    WHEN 7 THEN 18        
    WHEN 8 THEN 18        
    WHEN 9 THEN 18        
    WHEN 10 THEN 19        
    WHEN 11 THEN 20    
    End
where oildatasetlabstatusid in 
(
                select oildatasetstatusid
                from OilDataSetStatus
                inner join OilDataSet on OilDataSet.OilDataSetID = 
                    OilDataSetStatus.OilDataSetID
                where SamplePointID in 
                (
                                select SamplePointID 
                                from SamplePoint
                                where CustomerSiteID in
                                (
                                                select CustomerSiteID
                                                from CustomerSite
                                                where CustomerID = 2
                                )
                )
)

4 个答案:

答案 0 :(得分:4)

我认为您错过了要在CASE语句中评估的语句。

update oildatasetstatus set oildatasetstatusid =
case oildatasetstatusid
 WHEN 5 THEN 16
 WHEN 6 THEN 17
 WHEN 7 THEN 18
 WHEN 8 THEN 18
 WHEN 9 THEN 18
 WHEN 10 THEN 19
 WHEN 11 THEN 20
End
where oildatasetlabstatusid in ( select oildatasetstatusid from OilDataSetStatus inner join OilDataSet on OilDataSet.OilDataSetID = OilDataSetStatus.OilDataSetID where SamplePointID in ( select SamplePointID from SamplePoint where CustomerSiteID in ( select CustomerSiteID from CustomerSite where CustomerID = 2 ) ) )

试一试?

答案 1 :(得分:3)

您的案例陈述没有可供使用的对象。

你可以通过两种方式做到:

set oildatasetstatusid = 
    case oildatasetstatusid
    WHEN 5 THEN 16        
    WHEN 6 THEN 17        
    WHEN 7 THEN 18        
    WHEN 8 THEN 18        
    WHEN 9 THEN 18        
    WHEN 10 THEN 19        
    WHEN 11 THEN 20    
    End

set oildatasetstatusid = 
    case 
    WHEN oildatasetstatusid = 5 THEN 16        
    WHEN oildatasetstatusid = 6 THEN 17        
    WHEN oildatasetstatusid = 7 THEN 18        
    WHEN oildatasetstatusid = 8 THEN 18        
    WHEN oildatasetstatusid = 9 THEN 18        
    WHEN oildatasetstatusid = 10 THEN 19        
    WHEN oildatasetstatusid = 11 THEN 20    
    End

答案 2 :(得分:2)

看起来你的case语句需要指定针对给定值测试哪个列。

例如:

 update  oildatasetstatus
    set     oildatasetstatusid = case WHEN oildatasetstatusid = 5 THEN 16
                                      WHEN oildatasetstatusid = 6 THEN 17
                                      WHEN oildatasetstatusid = 7 THEN 18
                                      WHEN oildatasetstatusid = 8 THEN 18
                                      WHEN oildatasetstatusid = 9 THEN 18
                                      WHEN oildatasetstatusid = 10 THEN 19
                                      WHEN oildatasetstatusid = 11 THEN 20
                                 End
    where   oildatasetlabstatusid in (
            select  oildatasetstatusid
            from    OilDataSetStatus
                    inner join OilDataSet on OilDataSet.OilDataSetID = OilDataSetStatus.OilDataSetID
            where   SamplePointID in (
                    select  SamplePointID
                    from    SamplePoint
                    where   CustomerSiteID in ( select  CustomerSiteID
                                                from    CustomerSite
                                                where   CustomerID = 2 ) ) )

答案 3 :(得分:2)

您现在编写语句的方式将起作用(一旦您添加对case语句的列引用,如其他帖子所述),但是,让其余的语法取消注释会对其他人造成伤害在你的情况下。

虽然您可能只需要运行一次此查询,但我和其他人遇到类似情况,其中Update到多行也依赖于数据3或4个表远离我们的源并且必须运行许多时间(如在报告中)。

通过将您的子选项折叠为单个select语句并将其结果保存到#Temp表或@Table变量中,您只需执行一次查找,然后从更新的结果集中选择。

以下是使用@table变量的示例:

declare @OilStatus table (oilDatasetStatusID int)
insert into @OilStatus
    select odss.oildatasetstatusid
    from OildataSetStatus odss
    join oilDataSet ods on ods.OilDataSetID = odss.OilDataSetID
    join SamplePoint sp on sp.SamplePointID = odss.SamplePointID
    join CustomerSite cs on cs.CustomerSiteID = sp.CustomerSiteID
    where cs.CustomerID = 2

update oildatasetstatus
set oildatasetstatusid = 
    case oildatasetstatusid
        WHEN 5 THEN 16        
        WHEN 6 THEN 17        
        WHEN 7 THEN 18        
        WHEN 8 THEN 18        
        WHEN 9 THEN 18        
        WHEN 10 THEN 19        
        WHEN 11 THEN 20    
end
where oildatasetlabstatusid in ( select oilDatasetStatusID from @OilStatus )

由于我没有您的确切模式,因此在尝试实施上述示例时可能会出现错误,但我认为您会明白这一点。

此外,只要在单个语句中使用多个表,请尝试在每个列名前加上别名或完整的表名。它有助于保持sql引擎和阅读代码的人免于迷失。