我想从表'插入'向NewTable插入一些id(由calledMethod找到),当这个id实际上它不在这个表中时(NewTable) 实际上我正在使用这种方法(称为方法)两次。如何使用例如别名减少这个?
CREATE TRIGGER TriggerName
ON Table
AFTER INSERT
AS
BEGIN
INSERT INTO NewTable
(
FirstId
SecondId
)
SELECT
I.ID
CalledMethod(I.Name)
FROM INSERTED I
WHERE CalledMethod(I.Name)
NOT IN (SELECT SecondId FROM NewTable)
END
GO
当我想要同时插入两行时,会出现第二个问题。
Insert Into Table
(
Name
)
Values
('ro'),('ro-RO')
在这种情况下,该方法返回相同的索引,并且两者都将被添加。如何解决这个问题。
在这种情况下,该方法返回相同的索引
这是calledMethod
的示例CREATE FUNCTION CalledMethod
(
@internalName nvarchar(50)
)
RETURNS int
AS
BEGIN
return case
when @internalName Like 'ro%' then 6
when @internalName Like 'sk%' then 7
when @internalName Like 'bg%' then 9
end
END
答案 0 :(得分:0)
我相信这就是你所需要的:
CREATE TRIGGER TriggerName
ON Table
AFTER INSERT
AS
BEGIN
INSERT INTO NewTable
(
FirstId
SecondId
)
SELECT
min(I.ID),
x.called
FROM INSERTED I
CROSS APPLY
(SELECT CalledMethod(I.Name) called) x
WHERE
x.called NOT IN (SELECT SecondId FROM NewTable)
GROUP BY x.called
END