use [lansweeperdb]
go
drop trigger dbo.UpdateChildAsset
go
CREATE TRIGGER dbo.UpdateChildAsset
ON tblAssetCustom
AFTER UPDATE As
BEGIN
SET NOCOUNT ON;
DECLARE @Building nvarchar(max),
@Department nvarchar(max),
@ParentAssetID int;
SELECT @Building = Building, @Department = Department FROM inserted;
SELECT @ParentAssetID = AssetID FROM inserted;
UPDATE tblAssetCustom SET Building = @Building, Department = @Department where tblAssetCustom.AssetID in
(Select tblAssetRelations.ChildAssetID from tblAssetRelations
WHERE tblAssetRelations.ParentAssetID = @ParentAssetID
)
END
drop trigger dbo.UpdateAssetFromParent
go
CREATE TRIGGER dbo.UpdateAssetFromParent
ON tblAssetRelations
AFTER INSERT As
BEGIN
SET NOCOUNT ON;
DECLARE @Building nvarchar(max),
@Department nvarchar(max);
DECLARE @ChildAssetID int;
SELECT Top 1 @Building = Building, @Department = Department FROM inserted, tblAssetCustom where tblAssetCustom.AssetID = inserted.ParentAssetID;
SELECT @ChildAssetID = ChildAssetID FROM inserted;
UPDATE tblAssetCustom SET Building = @Building, Department = @Department where tblAssetCustom.AssetID = @ChildAssetID;
END