SQL: Insert Into with multiple case whens - 语法错误
SQL: Insert Into with multiple case whens - syntax error
我在 "then" 上有一个语法错误,我不确定为什么。
我正在尝试在 sp 中插入 4 个数据,其中两个是日期函数,最后一个是根据 table 上实际存在的内容进行硬编码的。
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))
我猜我不能把最后一个值放在 then 之后,所以我应该声明一个变量吗? (在 select 之前无法这样做)。
编辑 - 现在做了一些修改,我收到一个 DBengine 错误,说我正在尝试向 customergroupid 字段插入一个空值?没有意义。
谢谢
只是更正您的语法,您在每个 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
不幸的是,我认为纠正您的语句的语法是不够的,因为您的实际情况没有任何实际意义:您所说的本质上是:
If a record exists in my table where no record exists in my table for
this customer with a group ID of 1, then the CustomerGroupID should
equal 33
看到那里的冗余了吗?您能否添加评论来准确描述您尝试为 CustomerGroupID 实施的条件?
我在 "then" 上有一个语法错误,我不确定为什么。 我正在尝试在 sp 中插入 4 个数据,其中两个是日期函数,最后一个是根据 table 上实际存在的内容进行硬编码的。
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))
我猜我不能把最后一个值放在 then 之后,所以我应该声明一个变量吗? (在 select 之前无法这样做)。
编辑 - 现在做了一些修改,我收到一个 DBengine 错误,说我正在尝试向 customergroupid 字段插入一个空值?没有意义。
谢谢
只是更正您的语法,您在每个 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
不幸的是,我认为纠正您的语句的语法是不够的,因为您的实际情况没有任何实际意义:您所说的本质上是:
If a record exists in my table where no record exists in my table for this customer with a group ID of 1, then the CustomerGroupID should equal 33
看到那里的冗余了吗?您能否添加评论来准确描述您尝试为 CustomerGroupID 实施的条件?