我有一个存储过程,该过程将XML字符串作为参数。该XML将可变数量的记录插入表中。
但是要插入的表具有触发器,因此每次插入时,另一个表都会发生其他事情。
我面临的问题是它将XML插入视为单行插入,即使通过该过程插入了10条记录,触发器也只能运行一次。
有没有办法解决此问题,仍然使用触发器,还是我需要走另一条路线?
@departments XML = <departments>
<department>
<departmentID>123</departmentID>
</department>
<department>
<departmentID>456</departmentID>
</department>
</departments>
-- Add new department into primary table
INSERT INTO careerpath.LatticeDepartment (LatticeID,DepartmentID,DepartmentOrder)
SELECT @latticeID,
ParamValues.x1.value('departmentID[1]', 'int'),
99
FROM @departments.nodes('/departments/department') AS ParamValues(x1)
表cp。部门有一个触发器
即使该存储过程将10条记录插入careerpath.LatticeDepartment
中,它也只能运行一次。
-- Insert statements for trigger here
DECLARE @latticeID int
DECLARE @departmentID INT
SELECT @latticeID = Inserted.LatticeID, @departmentID = Inserted.DepartmentID
FROM Inserted
-- count number of titles
DECLARE @numTitles INT
SELECT @numTitles = COUNT(TitleID)
FROM careerpath.LatticeTitle
WHERE LatticeID = @latticeID
-- check if there are titles with same latticeID
IF @numTitles >= 1
BEGIN
-- create new boxes for all titleID's with given latticeID and DepartmentID
INSERT INTO careerpath.Box
(
LatticeID,
DepartmentID,
TitleID
)
SELECT @latticeID,
@departmentID,
TitleID
FROM careerpath.LatticeTitle
WHERE LatticeID = @latticeID
END