Trigger&Procedure的應用

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[tg_user_add] ' and   OBJECTPROPERTY (id, N ' IsTrigger ' =   1 )
drop   trigger   [ dbo ] . [ tg_user_add ]
GO

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[tg_user_edit] ' and   OBJECTPROPERTY (id, N ' IsTrigger ' =   1 )
drop   trigger   [ dbo ] . [ tg_user_edit ]
GO

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[tg_test] ' and   OBJECTPROPERTY (id, N ' IsTrigger ' =   1 )
drop   trigger   [ dbo ] . [ tg_test ]
GO

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[sp_output] ' and   OBJECTPROPERTY (id, N ' IsProcedure ' =   1 )
drop   procedure   [ dbo ] . [ sp_output ]
GO

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[sp_output_return] ' and   OBJECTPROPERTY (id, N ' IsProcedure ' =   1 )
drop   procedure   [ dbo ] . [ sp_output_return ]
GO

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[sp_return] ' and   OBJECTPROPERTY (id, N ' IsProcedure ' =   1 )
drop   procedure   [ dbo ] . [ sp_return ]
GO

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[test_output] ' and   OBJECTPROPERTY (id, N ' IsProcedure ' =   1 )
drop   procedure   [ dbo ] . [ test_output ]
GO

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[test_output_return] ' and   OBJECTPROPERTY (id, N ' IsProcedure ' =   1 )
drop   procedure   [ dbo ] . [ test_output_return ]
GO

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[test_return] ' and   OBJECTPROPERTY (id, N ' IsProcedure ' =   1 )
drop   procedure   [ dbo ] . [ test_return ]
GO

if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[user] ' and   OBJECTPROPERTY (id, N ' IsUserTable ' =   1 )
drop   table   [ dbo ] . [ user ]
GO

CREATE   TABLE   [ dbo ] . [ user ]  (
    
[ id ]   [ char ]  ( 10 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NOT   NULL  ,
    
[ name ]   [ varchar ]  ( 10 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NOT   NULL  ,
    
[ ename ]   [ varchar ]  ( 50 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NOT   NULL  ,
    
[ email ]   [ varchar ]  ( 50 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NOT   NULL  ,
    
[ tel ]   [ varchar ]  ( 20 ) COLLATE Chinese_Taiwan_Stroke_CI_AS  NOT   NULL  
ON   [ PRIMARY ]
GO

ALTER   TABLE   [ dbo ] . [ user ]   WITH   NOCHECK   ADD  
    
CONSTRAINT   [ PK_user ]   PRIMARY   KEY    CLUSTERED  
    (
        
[ id ]
    )  
ON   [ PRIMARY ]  
GO

SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  OFF  
GO

CREATE   PROCEDURE   [ dbo ] . [ sp_output ]
(
   
@i   char ( 1 ) = ' 1 ' ,
   
@a   varchar ( 5 ) = null  output
)
 
AS
begin
  
if ( @i = ' 1 ' )
    
set   @a = ' aaaaa '
  
else
    
set   @a = ' bbbbb '
end
GO
SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  OFF  
GO

CREATE   PROCEDURE   [ dbo ] . [ sp_output_return ]
(
   
@i   char ( 1 ) = ' 1 ' ,
   
@a   varchar ( 5 ) = null  output
)
 
AS
begin
  
if ( @i = ' 1 ' )
  
begin
    
set   @a = ' aaaaa '
    
return   1
  
end
  
else
  
begin
    
set   @a = ' bbbbb '
    
return   0
  
end
end
GO
SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  OFF  
GO

CREATE   PROCEDURE   [ dbo ] . [ sp_return ]
(
   
@i   char ( 1 ) = ' 1 '
)
 
AS
begin
  
/*
  --return只能返回一個int類型的值,相反output則可以傳回更多類型的值
  無條件退出查詢或程序。RETURN 是立即而完整的,而且可在任何時刻用於退出程序、批次,或陳述式封鎖。
  其中附隨有 RETURN 的陳述式不會被執行。
  
*/
  
  
if ( @i = ' 1 ' )
    
return   1
  
else
    
return   0
end
GO
SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  OFF  
GO

CREATE   PROCEDURE   [ dbo ] . [ test_output ]
(
  
@i   char ( 1 ) = ' 1 '
)
 
AS
begin
  
declare   @b   varchar ( 5 )
  
exec  sp_output  @i , @b  output
  
-- exec @b=sp_output @i,@b output
   print   @b
end
GO
SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  OFF  
GO

CREATE   PROCEDURE   [ dbo ] . [ test_output_return ]
(
  
@i   char ( 1 ) = ' 1 '
)
 
AS
begin
  
declare   @b   int
  
declare   @a   varchar ( 5 )
  
exec   @b = sp_output_return  @i , @a  output
  
print ( @a )
  
print ( @b )
end
GO
SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  OFF  
GO

CREATE   PROCEDURE   [ dbo ] . [ test_return ]
(
  
@i   char ( 1 ) = ' 1 '
)
 
AS
begin
  
declare   @b   varchar ( 5 )
  
exec   @b = sp_return  @i
  
if  ( @b = 1 )
    
print ( ' aaaaa ' )
  
else
    
print ( ' bbbbb ' )
end
GO
SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

CREATE   TRIGGER   [ tg_user_add ]   ON   [ dbo ] . [ user ]  
FOR   INSERT  
AS
begin
  
declare   @id   char ( 10 )
  
declare   @name   varchar ( 10 )
  
declare   @ename   varchar ( 50 )
  
declare   @email   varchar ( 50 )
  
declare   @tel   varchar ( 20 )
  
declare   @sql   varchar ( 500 )
  
declare  user_cr  cursor   for    select   [ id ] , [ name ] , [ ename ] , [ email ] , [ tel ]   from  inserted
  
open  user_cr
  
fetch   next   from  user_cr
  
into   @id , @name , @ename , @email , @tel
  
while   @@fetch_status = 0
  
begin
    
set   @sql = ' insert into [user] (id,name,ename,email,tel) values ( '''   +   @id   +   ''' , '''   +   @name   +   ''' , '''   +   @ename   +   ''' , '''   +   @email   +   ''' , '''   +   @tel   +   ''' ) '
    
print ( @sql )
    
fetch   next   from  user_cr
    
into   @id , @name , @ename , @email , @tel
  
end
  
close  user_cr
  
deallocate  user_cr
  
select   *   from  deleted
  
select   *   from  inserted
end





GO
SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

CREATE   TRIGGER   [ tg_user_edit ]   ON   [ dbo ] . [ user ]  
FOR   update
AS
begin
  
declare   @id   char ( 10 )
  
declare   @name   varchar ( 10 )
  
declare   @ename   varchar ( 50 )
  
declare   @email   varchar ( 50 )
  
declare   @tel   varchar ( 20 )
  
declare   @sql   varchar ( 500 )
  
declare  user_cr  cursor   for    select   [ id ] , [ name ] , [ ename ] , [ email ] , [ tel ]   from  inserted
  
open  user_cr
  
fetch   next   from  user_cr
  
into   @id , @name , @ename , @email , @tel
  
while   @@fetch_status = 0
  
begin
    
set   @sql = ' insert into [user] (id,name,ename,email,tel) values ( '''   +   @id   +   ''' , '''   +   @name   +   ''' , '''   +   @ename   +   ''' , '''   +   @email   +   ''' , '''   +   @tel   +   ''' ) '
    
print ( @sql )
    
fetch   next   from  user_cr
    
into   @id , @name , @ename , @email , @tel
  
end
  
close  user_cr
  
deallocate  user_cr
  
select   *   from  deleted
  
select   *   from  inserted
  
exec  test_output_return
end





GO
SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

CREATE   TRIGGER   [ tg_test ]   ON   [ dbo ] . [ user ]  
FOR   INSERT UPDATE DELETE  
AS
  
print ( ' 1111111111 ' )
GO
SET  QUOTED_IDENTIFIER  OFF  
GO
SET  ANSI_NULLS  ON  
GO

說明:
1,trigger中可以調用procedure.而procedure中不可以調用tiegger

你可能感兴趣的:(procedure)