CASE Statement inside CHECK Constraint Error: An expression of non-boolean type specified in a context where a condition is expected, near ')'
CASE Statement inside CHECK Constraint Error: An expression of non-boolean type specified in a context where a condition is expected, near ')'
我试图在 SQL 服务器中的 CHECK
约束内使用 CASE
语句,但出现此错误:
Msg 4145, Level 15, State 1, Line 15
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
create table dbo.Advertisement
(
advertisement_id int NOT NULL PRIMARY KEY,
roadside_bottom varchar (20),
roadside_top_bottom varchar (20),
curbside varchar (20),
rearside varchar (20),
headliner varchar(10),
bus varchar (10),
CONSTRAINT bottom_pass
CHECK(CASE
WHEN roadside_bottom = 'King' AND roadside_top_bottom IS NULL THEN 1
WHEN roadside_bottom = 'Super King' and roadside_top_bottom IS NULL THEN 1
END)
);
我需要语法方面的帮助。
您可以使用 case
,但它 returns 是一个值,需要与以下内容进行比较:
CONSTRAINT bottom_pass CHECK
(
CASE WHEN roadside_bottom = 'King' AND roadside_top_bottom IS NULL THEN 1
WHEN roadside_bottom = 'Super King' and roadside_top_bottom IS NULL THEN 1
END = 1
)
然而,这将更简洁地写成:
CONSTRAINT bottom_pass CHECK
(
roadside_top_bottom IS NULL AND
roadside_bottom IN ('King', 'Super King')
)
我试图在 SQL 服务器中的 CHECK
约束内使用 CASE
语句,但出现此错误:
Msg 4145, Level 15, State 1, Line 15
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
create table dbo.Advertisement
(
advertisement_id int NOT NULL PRIMARY KEY,
roadside_bottom varchar (20),
roadside_top_bottom varchar (20),
curbside varchar (20),
rearside varchar (20),
headliner varchar(10),
bus varchar (10),
CONSTRAINT bottom_pass
CHECK(CASE
WHEN roadside_bottom = 'King' AND roadside_top_bottom IS NULL THEN 1
WHEN roadside_bottom = 'Super King' and roadside_top_bottom IS NULL THEN 1
END)
);
我需要语法方面的帮助。
您可以使用 case
,但它 returns 是一个值,需要与以下内容进行比较:
CONSTRAINT bottom_pass CHECK
(
CASE WHEN roadside_bottom = 'King' AND roadside_top_bottom IS NULL THEN 1
WHEN roadside_bottom = 'Super King' and roadside_top_bottom IS NULL THEN 1
END = 1
)
然而,这将更简洁地写成:
CONSTRAINT bottom_pass CHECK
(
roadside_top_bottom IS NULL AND
roadside_bottom IN ('King', 'Super King')
)