SQL:使用多个case whens插入到 - 语法错误

时间:2015-03-16 17:27:58

标签: sql sql-server tsql

我在“then”上有语法错误,我不知道为什么。 我正在尝试在sp中插入4个数据,其中两个是日期函数,最后一个是根据表中实际存在的内容进行硬编码。

insert into tblcustomer 
  (
    customerid,  
    customerjoinDate, 
    customerexpiryDate, 
    customergroupid
  )
select 
    @person_id, 
    GetDate(), 
    dateadd (year, 1, GetDate()), 
    case            
        when exists
            (

                    select * 
                    from tblcustomer 
                    where 
                        customerid = @person_id and 
                        customergroupid = 35
             )
        then 1            
        when exists
            (
                    select * 
                    from tblcustomer 
                    where 
                        customerid = @person_id and 
                        customergroupid = 33

            )
        then 2            
        when exists
            (

                    select * 
                    from tblcustomer 
                    where 
                        customerid = @person_id and 
                        customergroupid = 37

            )
        then 3          
        when exists
            (

                    select * 
                    from tblcustomer 
                    where 
                        customerid = @person_id and 
                        customergroupid = 36

            )
        then 4
        when exists
            (

                    select * 
                    from tblcustomer 
                    where 
                        customerid = @person_id and 
                        customergroupid = 34

            )
        then 5               

    END 
from tblcustomer
where NOT EXISTS (select 2 from tblcustomer where customerid = @person_id and customergroupid IN (1, 2, 3, 4, 5))

我猜我不能在那之后放上最后一个值,所以我应该声明一个变量吗? (在选择之前无法这样做)。

编辑 - 现在有些修改了,我得到一个DBengine错误,说我正在尝试在customergroupid字段中插入一个空值?没有意义。

由于

1 个答案:

答案 0 :(得分:0)

只需更正语法,在每次WHERE NOT EXISTS配对后都缺少一个括号,并且注释中指出的... else end case ...结构是错误的。如果我要修复这两个错误,SQL将如下所示:

insert into tblcustomer 
  (
    customerid, 
    customerjoinDate, 
    customerexpiryDate, 
    customergroupid
  )
select 
    @membership_id, 
    GetDate(), 
    dateadd (year, 1, GetDate()), 
    case            
        when exists
            (
                select customergroupid 
                from tblcustomer 
                where not exists
                    (
                    select * 
                    from tblcustomer 
                    where 
                        (customerid = @membership_id and 
                        customergroupid = 1)
                    )
            )
        then 33            
        when exists
            (
                select customergroupid 
                from tblcustomer 
                where not exists
                    (
                    select * 
                    from tblcustomer 
                    where 
                        (customerid = @membership_id and 
                        customergroupid = 2)
                    )
            )
        then 34            
        when exists
            (
                select customergroupid 
                from tblcustomer 
                where not exists
                    (
                    select * 
                    from tblcustomer 
                    where 
                        (customerid = @membership_id and 
                        customergroupid = 3)
                    )
            )
        then 35          
        when exists
            (
                select customergroupid 
                from tblcustomer 
                where not exists
                    (
                    select * 
                    from tblcustomer 
                    where 
                        (customerid = @membership_id and 
                        customergroupid = 4)
                    )
            )
        then 36         
    END 
from tblcustomer

不幸的是,我不认为纠正你的陈述的语法就足够了,因为你的实际情况没有任何意义:你所说的基本上是:

  

如果我的表中存在记录,我的表中没有记录   这个客户的组ID为1,那么CustomerGroupID应该是   等于33

看那里的冗余?您是否可以添加一条注释,准确描述您尝试为CustomerGroupID实现的条件?