我需要生成TSQL
这样的查询:
IF @GenderOfEmployee IS NOT NULL
CASE @GenderOfEmployee = 1
THEN INSERT INTO @NotAllowedGenderOfJobPost (TagID) values (139)
ELSE INSERT INTO @NotAllowedGenderOfJobPost (TagID) values (138)
END;
我的意思是我希望CASE
声明中有IF
声明
可能吗?
考虑到我不想要其他类似的方式来提供该查询。
感谢您的高级帮助。
答案 0 :(得分:1)
像这样改变
IF @GenderOfEmployee IS NOT NULL
BEGIN
INSERT INTO @NotAllowedGenderOfJobPost (TagID) SELECT CASE WHEN @GenderOfEmployee = 1
THEN 139
ELSE 138
END
END;
答案 1 :(得分:0)
select
无需插入case
表达式的结果:
declare @NotAllowedGenderOfJobPost as Table ( TagId Int );
declare @GenderOfEmployee as Int = NULL;
-- Try NULL.
if @GenderOfEmployee is not NULL
insert into @NotAllowedGenderOfJobPost (TagId ) values
( case when @GenderOfEmployee = 1 then 139 else 138 end );
select * from @NotAllowedGenderOfJobPost;
-- Try 0.
set @GenderOfEmployee = 0;
if @GenderOfEmployee is not NULL
insert into @NotAllowedGenderOfJobPost (TagId ) values
( case when @GenderOfEmployee = 1 then 139 else 138 end );
select * from @NotAllowedGenderOfJobPost;
-- Try 1.
set @GenderOfEmployee = 1;
if @GenderOfEmployee is not NULL
insert into @NotAllowedGenderOfJobPost (TagId ) values
( case when @GenderOfEmployee = 1 then 139 else 138 end );
select * from @NotAllowedGenderOfJobPost;