Syntax of triggers, stored procedures, views, transactions

1. Trigger

Create trigger User_OnUpdate  
    On user_Users  
    for Update
As
    declare @msg nvarchar(50)
    --@msg record modification
    select @msg = N' name from "' + Deleted.Name + N'" to "' + Inserted.Name + '"' from Inserted,Deleted
    --Insert log table
    insert into [LOG](MSG)values(@msg)
      
--delete trigger
drop trigger User_OnUpdate

2. Stored procedure ( procedure can be abbreviated as proc )

--Create a stored procedure with an output parameter
CREATE PROCEDURE PR_Sum
    @a int,
    @b int,
    @sum int output
AS
BEGIN
    set @sum=@a+@b
END
  
--Create a Return return value stored procedure
CREATE PROCEDURE PR_Sum2
    @a int,
    @b int
AS
BEGIN
    Return @a+@b
END
      
--Execute the stored procedure to get the output return value
declare @mysum int
execute PR_Sum 1,2,@mysum output
print @mysum
  
--Execute the stored procedure to get the return return value
declare @mysum2 int
execute @mysum2= PR_Sum2 1,2
print @mysum2

3. View

 
 
-- create a normal view
create view View_Classasselect dbo.t_table1.id,dbo.t_table1.name,dbo.t_table2.id,dbo.t_table2.namefrom dbo.t_table1,dbo.t_table2where dbo.t_table1.id = dbo.t_table2.idgo
Create encrypted view

create view View_Class
with encryption
as
select dbo.t_table1.id,dbo.t_table1.name,dbo.t_table2.id,dbo.t_table2.name
from dbo.t_table1,dbo.t_table2
where dbo.t_table1.id = dbo.t_table2.id
go

Modify view

alter view View_Class
as
select dbo.t_table1.id,dbo.t_table1.address,dbo.t_table2.id,dbo.t_table2.name
from dbo.t_table1,dbo.t_table2
where dbo.t_table1.id = dbo.t_table2.id
go

Modify view

if object_id('View_Class','view') is not null
drop view View_Class
go

4. The transaction starts with BEGIN TRAN, if it is committed, COMMIT commits the transaction, otherwise it rolls back the transaction with ROLLBACK

--define transaction
BEGIN TRAN;
  INSERT INTO dbo.T1(keycol, col1, col2) VALUES(4,101,'C');
  INSERT INTO dbo.T1(keycol, col1, col2) VALUES(4,201,'X');
COMMIT TRAN;





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326272263&siteId=291194637