我正在尝试创建一个触发器,以便在另一个表中创建记录时将记录插入到一个表中。我已经想出了如何做到这一点,但我想弄清楚如何更改其中一个列值的值。
在原始表格中我有'县'在第二个表格中我有一个' CountyID'来自一个县'表。我该如何选择'郡'并将其转换为' CountyID'?
到目前为止我的触发器 -
CREATE TRIGGER [dbo].[AQB_RMS_LocationCreate] on [AVData].[dbo].[SourceSite]
AFTER INSERT
AS
BEGIN
INSERT INTO AQB_MON.[AQB_RMS].[Location]
([LocationName],[Latitude],[Longitude],[Enabled]
,[StreetAddress1],[StreetAddress2],[City],[CountyID],[StateAbbr],[ZipCode],[DATE_CREATED])
SELECT i.[Description],i.[Latitude],i.[Longitude],i.[Enabled]
,i.[StreetAddress1],i.[StreetAddress2],i.[City],i.[County],i.[StateRegion],i.[ZipCode], getdate()
from [AVData].[dbo].[SourceSite] ss
inner join inserted i on ss.SourceSiteID = i.SourceSiteID
END
GO
答案 0 :(得分:1)
这不会起作用吗?
CREATE TRIGGER [dbo].[AQB_RMS_LocationCreate] on [AVData].[dbo].[SourceSite]
AFTER INSERT
AS
BEGIN
INSERT INTO AQB_MON.[AQB_RMS].[Location]
([LocationName],[Latitude],[Longitude],[Enabled]
,[StreetAddress1],[StreetAddress2],[City],[CountyID],[StateAbbr],[ZipCode],[DATE_CREATED])
SELECT i.[Description],i.[Latitude],i.[Longitude],i.[Enabled]
,i.[StreetAddress1],i.[StreetAddress2],i.[City],C.CountyId,i.[StateRegion],i.[ZipCode], getdate()
from [AVData].[dbo].[SourceSite] ss
inner join inserted i on ss.SourceSiteID = i.SourceSiteID
inner join dbo.County C on C.County = i.County
END