An example of the trigger of the View.

xiaoxiao2021-03-06  141

/ * CREATE TABLETABLE (Userid Uniqueidentifier NOT NULL ROWGUIDCOL, - Unique Identification Name NVARCHAR (60) Null, - Name

Age Int Null Default 0 Check (age> 20), - Age Address NVARCHAR (25) NULL - Address) * / GO

Create Table LogTable11 (Name Nvarchar (60) Null, Age Int) Gocreate Table LogTable2 (Name NVARCHAR (60) NULL, AGE INT)

GoInsert LogTable1 Values ​​('T11', 30)

INSERT logTable1 Values ​​('T12', 40)

INSERT logTable1 Values ​​('T13', 50)

INSERT logTable2 Values ​​('T21', 130)

INSERT logTable2 Values ​​('T22', 140)

Insert LogTable2 Values ​​('T23', 150) Insert LogTable2 Values ​​('T24', 160)

SELECT * INTO logall from logtable1 union selection * from logtable2

Go - Create ViewCreate View Logview As SELECT * FROM LOGTABLE1 Union Select * from logtable2

--Create Trigger of Delete In View_Delete on logview instead of deleteasbegin delete logall Where name in (select name from deleted) end Go

--Create Trigger of Insert in View_Insert ON Logview Instead of InsertsBegin Insert Logall Select * from InsertEDEND GO

--create trigger of update in viewcreate trigger logView_update on logView instead of (20) declare @oldname varchar updateasbegin set nocount off if not exists (select * from inserted) return declare @name varchar (20) declare @age int

SELECT @name = name, @ agn = age from inserted select @ oldname = name from deleta

update logtable1 set name = @ name, age = @ age where name = @ oldname update logtable2 set name = @ name, age = @ age where name = @ oldname / * if @namer is null and @age is null begin raiserror (1074008065 , 16, 127) with NOWAIT, SETERRR RETURN Endif (Update (Name)) - Begin Exec P_GetName End * /

end

--Testdelete logview where name = 't11'

Insert Logview Values ​​('V1', 100)

Update logview set name = 'T1212', AGE = 10000 Where name = 't12'

转载请注明原文地址:https://www.9cbs.com/read-127252.html

New Post(0)