很抱歉,没有足够的信息,但是我仍在学习SQL,希望对您有所帮助。
我想在视图上设置触发器,该触发器在更新或插入后激活,然后更新另一个表上的特定列。我拥有这两个数据集的唯一方法是从日期和人的名字开始。否则,他们不会共享任何其他信息。
答案 0 :(得分:0)
这就是我发现的工作方式,感谢您提供的任何帮助。这可能并不理想,因此,如果有任何建议,请随时发表评论。
CREATE TRIGGER triggername
ON TABLE1
AFTER INSERT, UPDATE
AS
BEGIN
DECLARE @INS NVARCHAR(50)
SELECT @INS = NAMECOLUMN FROM INSERTED
WHERE DESCRIPTION = 'BRIEF DESCRIPTION'
END
IF @INS = ‘Last, First’
BEGIN
UPDATE TABLE1
SET DESTINATIONCOLUMN = EXAMPLE.CT
FROM (SELECT COUNT(*) AS CT FROM TABLE2 WHERE NAMECOLUMN = ‘Last, First’) EXAMPLE
WHERE DESCRIPTION = 'BRIEF DESCRIPTION' AND NAMECOLUMN = ‘Last, First’
END
BEGIN
答案 1 :(得分:-1)
TRIGGER
USE [DATABASENAME]
GO
CREATE TRIGGER [TRIGGERNAME] ON [VIEWNAME]
INSTEAD OF INSERT, UPDATE, DELETE
AS
IF @@ROWCOUNT = 0 -- exit trigger
RETURN;
IF EXISTS(SELECT * FROM inserted)
BEGIN
IF EXISTS(SELECT * FROM deleted)
-- to update in another table
UPDATE a
SET a.xyz = i.xyz
FROM another_table a
INNER JOIN inserted i
ON a.column = i.column
ELSE
-- to insert in another table
INSERT INTO [dbo].[another_table]
SELECT * FROM inserted
END
ELSE
BEGIN
-- to delete from another table
DELETE a
FROM another_table a
INNER JOIN deleted d
ON a.column = d.column
END