Sql Delete触发器中的多个IF语句
我正在尝试在SQL Server触发器中创建以下逻辑: 检查是否已删除。ProfID=3 如果是,请检查tblPartLic中是否没有ProfID=3的记录,然后 如果没有,请在tblParticipantLicense中插入未授权的3记录 -a。idsNumber,txtEmailAddress,state=70 state 我试过:Sql Delete触发器中的多个IF语句,sql,sql-server,database-trigger,nested-if,Sql,Sql Server,Database Trigger,Nested If,我正在尝试在SQL Server触发器中创建以下逻辑: 检查是否已删除。ProfID=3 如果是,请检查tblPartLic中是否没有ProfID=3的记录,然后 如果没有,请在tblParticipantLicense中插入未授权的3记录 -a。idsNumber,txtEmailAddress,state=70 state 我试过: IF (deleted.idsProf = 3) BEGIN IF NOT Exists(Select PL.idsNumber FROM tblPa
IF (deleted.idsProf = 3)
BEGIN
IF NOT Exists(Select PL.idsNumber FROM tblPartLic PL INNER JOIN Deleted D
ON PL.idsNumber=D.idsNumber
WHERE PL.idsProfession = 3 AND PL.idsState <> 70)
BEGIN
INSERT INTO tblPartLic (idsNumber, txtLicNumber, txtState, txtProfOrg,
idsCountry, idsState, idsProf)
SELECT Deleted.idsDASNumber, 'AR NL', 'NL', 'Architect', 208, 70, 3
FROM Deleted
END;
END;
Deleted是一个表伪表,因此可能有多行-其中一些行的ProfID=3,一些行的ProfID为3。这是必须处理的。此外,关系数据库是为基于集合的操作而设计的,而不是为过程操作而设计的。您的逻辑可以用适当的where子句构建到单个insert语句中。以下内容复制了您上面显示的内容:
INSERT INTO tblPartLic (idsNumber, txtLicNumber, txtState, txtProfOrg, idsCountry, idsState, idsProf)
SELECT D.idsDASNumber, 'AR NL', 'NL', 'Architect', 208, 70, 3
FROM Deleted D
-- Condition 1 - ProfID = 3
WHERE D.ProfID = 3
-- Condition 2 - No existing record in tblPartLic
AND NOT EXISTS (
SELECT PL.idsNumber
FROM tblPartLic PL
WHERE PL.idsProfession = 3 AND PL.idsState <> 70
AND PL.idsNumber = D.idsNumber
);
Deleted是一个表伪表,因此可能有多行-其中一些行的ProfID=3,一些行的ProfID为3。这是必须处理的。此外,关系数据库是为基于集合的操作而设计的,而不是为过程操作而设计的。您的逻辑可以用适当的where子句构建到单个insert语句中。以下内容复制了您上面显示的内容:
INSERT INTO tblPartLic (idsNumber, txtLicNumber, txtState, txtProfOrg, idsCountry, idsState, idsProf)
SELECT D.idsDASNumber, 'AR NL', 'NL', 'Architect', 208, 70, 3
FROM Deleted D
-- Condition 1 - ProfID = 3
WHERE D.ProfID = 3
-- Condition 2 - No existing record in tblPartLic
AND NOT EXISTS (
SELECT PL.idsNumber
FROM tblPartLic PL
WHERE PL.idsProfession = 3 AND PL.idsState <> 70
AND PL.idsNumber = D.idsNumber
);