我有3个表,并为所有三个表创建了视图。
表:s1
CREATE TABLE s1
(
Cola INT,
Colb VARCHAR(10)
);
表:s2
CREATE TABLE s2
(
Cola INT,
Colb VARCHAR(10)
);
表:s3
CREATE TABLE s3
(
Cola INT,
Colb VARCHAR(10)
);
插入:
INSERT INTO s1 VALUES(1,'a');
INSERT INTO s1 VALUES(1,'b');
INSERT INTO s2 VALUES(3,'c');
INSERT INTO s2 VALUES(4,'d');
INSERT INTO s3 VALUES(1,'a');
INSERT INTO s3 VALUES(1,'b');
创建视图:sx
CREATE VIEW sx as
SELECT * FROM s1
UNION ALL
SELECT * FROM s2
UNION ALL
SELECT * FROM s3;
现在我有了这个:
SELECT * FROM Sx;
结果:
-------------
cola colb
-------------
1 a
1 b
3 c
4 d
1 a
1 b
注意:我想要ALTER VIEW
删除其中cola
包含值1
的表格。根据我的说法,表s1
和s3
具有这些值。
注1 :如果所有表都包含值1
,则VIEW
必须为drop
。
问题:如何通过检查上面给出的具体条件来改变视图?
答案 0 :(得分:0)
您可以使用替代触发器:
Create Trigger SX_Del On Sx Instead of delete
As
Begin
Delete s From s1 s Inner Join deleted d on d.Cola = s.Cola
Delete From s2 Where cola in (Select Cola From deleted )
Delete From s3 Where cola in (Select Cola From deleted )
End
就像您在视图中的每个不同的选择一样,您必须为每个表定义必须删除的内容。