有人能指出为什么这个插入触发器没有将新行插入IVRTestB表吗?
If Object_ID('MyTrig3', 'TR') IS Not Null
Drop Trigger MyTrig3;
GO
Alter Trigger MyTrig3
On dbo.IVRTest
After Insert, update
AS
Begin
SET NOCOUNT ON;
Insert Into [dbo].[IVRTestB]
(IVRAID, IVRName, DayNumber, OpenFlag)
Select
'i.IVRAID', 'i.IVRName', 'i.DayNumber', 'i.OpenFlag'
From inserted i
INNER JOIN dbo.IVRTestB b
On i.IVRAID = b.IVRAID
END
答案 0 :(得分:2)
通过将Inserted
的每一列放入单引号,您可以有效地将字符串文字插入到目标表中 - 而不是列值!
请改用此代码:
INSERT INTO [dbo].[IVRTestB] (IVRAID, IVRName, DayNumber, OpenFlag)
SELECT
i.IVRAID, i.IVRName, i.DayNumber, i.OpenFlag -- *NO* single quotes here!!!!
FROM
inserted i
-- change this WHERE clause to insert those rows that AREN'T alredy in IVRTestB !
WHERE
i.IVRAID NOT IN (SELECT DISTINCT IVRAID FROM dbo.IVRTestB)
答案 1 :(得分:0)
Alter Trigger MyTrig3
On dbo.IVRTest
After Insert
AS
Begin
SET NOCOUNT ON;
IF EXISTS ( SELECT
1
FROM
INSERTED
WHERE
INSERTED.DayNumber IS NULL )
Insert Into
[dbo].[IVRTestB]
(IVRAID,
IVRName,
DayNumber,
OpenFlag)
Select
i.IVRAID,
i.IVRName,
i.DayNumber,
i.OpenFlag
From inserted i
WHERE
INSERTED.DayNumber IS NULL
END
答案 2 :(得分:0)
Mohan将
你改变了几乎工作,我只需要将not nulls更改为not null并将别名更改为I
Alter Trigger MyTrig3
On dbo.IVRTest
After Insert
AS
Begin
SET NOCOUNT ON;
IF EXISTS ( SELECT
1
FROM
INSERTED
WHERE
INSERTED.DayNumber IS NOT NULL )
Insert Into
[dbo].[IVRTestB]
(IVRAID,
IVRName,
DayNumber,
OpenFlag)
Select
i.IVRAID,
i.IVRName,
i.DayNumber,
i.OpenFlag
From inserted i
WHERE
i.DayNumber IS NOT NULL
END