在dbo.BOM
中插入新记录时,或者我的列BOM.Site
更新后,我想在BOM.Site
中查找dbo.SiteMap
值,然后将其恢复SiteMap.SiteName
值并将其插入BOM.SiteMap
列。所以我认为触发器将是要走的路,但它不起作用。 (如果触发器不是最好的配件请告诉我!)有什么建议吗?提前谢谢。
表:
=BOM=
id | Site | SiteMap
---+----------+------
1 | Mex |
2 | MXN |
3 | USA |
4 | Mex |
=SiteMap=
id | SiteName | Sitecode
---+----------+------
1 | Mexico | Mex
2 | Mexico | MXN
3 | USA | USA
4 | USA | United States of America
期望的输出:
=BOM=
id | Site | SiteMap
---+----------+------
1 | Mex | Mexico
2 | MXN | Mexico
3 | USA | United States of America
4 | Mex | Mexico
我尝试了几件不同的事情......
CREATE TRIGGER dbo.trMapBom on dbo.BOM
AFTER INSERT,UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF UPDATE([Site ])
BEGIN
UPDATE dbo.BOM
SET BOM.[Site Map] =
(
SELECT [SiteName]
from dbo.SiteMap
where [PlantCode] = [Site ]
)
END
END
GO
另一次尝试..
CREATE TRIGGER dbo.trMapBom on dbo.BOM
AFTER INSERT,UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE dbo.BOM set [Site ] = SiteMap.SiteName
from dbo.BOM
inner join dbo.SiteMap
on BOM.[Site ] = SiteMap.PlantCode
END
GO
答案 0 :(得分:0)
您应该在触发器中使用插入的行:
CREATE TRIGGER dbo.trMapBom on dbo.BOM
AFTER INSERT,UPDATE
AS
BEGIN
SET NOCOUNT ON;
UPDATE b
set [SiteMap] = sm.SiteName
from dbo.BOM b
inner join dbo.SiteMap sm
on b.[Site] = sm.SiteCode
inner join inserted i
on i.id = b.id
END
GO