gpt4 book ai didi

sql - 在 if 语句中添加 delete 语句

转载 作者:行者123 更新时间:2023-12-01 23:27:12 25 4
gpt4 key购买 nike

我正在尝试将删除语句添加到现有的 IF 语句中。

我试过在开头和结尾添加语句,但它不起作用。

这是我为更新和插入语句创建的现有代码。

IF EXISTS (SELECT * FROM [int_SocialAccess].[dbo].[Authorisers] where ID = {ID})
update [int_SocialAccess].[dbo].[Authorisers] set
Name = {Name},
Email = {Email},
Responsibility = {Responsibility},
IsHeadOfService = {IsHeadOfService},
DeleteRec = {DeleteRec}
where ID = {ID}
ELSE
insert into [int_SocialAccess].[dbo].[Authorisers]
values( {Name},
{Email},
{Responsibility},
{IsHeadOfService},
'no')

我正在尝试在更新语句之前添加我的删除语句,如果“DeleteRec”字段是,我想删除记录,否则更新它。

IF {DeleteRec} = 'yes'
DELETE FROM [int_SocialAccess].[dbo].[Authorisers]
where ID = {ID}
ELSE

任何迹象都很好。

最佳答案

如果在一个 if-else block 中需要多个 stmt,则需要用 BEGIN END 换行 -

IF EXISTS (SELECT * FROM [int_SocialAccess].[dbo].[Authorisers] where ID = {ID})
BEGIN
IF {DeleteRec} = 'yes'
DELETE FROM [int_SocialAccess].[dbo].[Authorisers] where ID = {ID}
ELSE
Update
END
ELSE
insert into [int_SocialAccess].[dbo].[Authorisers]
values( {Name},
{Email},
{Responsibility},
{IsHeadOfService},
'no')

关于sql - 在 if 语句中添加 delete 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55970813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com