zoukankan      html  css  js  c++  java
  • Sqlserver中存储过程,触发器,自定义函数

    Sqlserver中存储过程,触发器,自定义函数:

    1.

     触发器:是针对数据库表或数据库的特殊存储过程,在某些行为发生的时候就会被激活
     触发器的分类:
     DML触发器:发生在数据操作语言执行时触发执行的存储过程,Insert,Update,Delete
     After触发器:记录被改变之后激活执行
     Instead of触发器:记录被改变之前激活执行。
     DDL触发器:响应数据定义语言执行时触发执行的存储过程,一般用于执行数据库中的管理任务
     审核和规范数据库的操作;
     防止数据表结构被更改或删除

    2.

    http://www.2cto.com/database/201302/190839.html

    触发器中的临时表:

      Inserted 
      存放进行insert和update 操作后的数据 
      Deleted 
      存放进行delete 和update操作前的数据 
      1 --创建触发器 
      2 go
      3 Create  trigger  User_OnUpdate 
      4      On  stuinfo 
      5      for  Update 
      6 As 
      7      declare  @msg nvarchar(50) 
      8      --@msg记录修改情况 
      9 select  @msg = N '姓名从"'  + Deleted.stuName  + N '"修改为"'  + Inserted.stuName  +  '"'  from Inserted,Deleted 
     10 --插入日志表 
     11      insert  into  [LOG](MSG) values (@msg)
     12      --删除触发器 
     13 drop  trigger  User_OnUpdate 
     14 
     15 -----------------存储过程语法---------------------- 
     16 --创建带output参数的存储过程 
     17 go
     18 CREATE  PROCEDURE  PR_Sum 
     19      @a  int , 
     20      @b  int , 
     21      @ sumq int output 
     22 AS 
     23 BEGIN 
     24      set  @ sum =@a+@b 
     25 END 
     26   
     27 --创建Return返回值存储过程 
     28 go
     29 CREATE  PROCEDURE  PR_Sum2 
     30      @a  int , 
     31      @b  int 
     32 AS 
     33 BEGIN 
     34      Return  @a+@b 
     35 END 
     36       
     37 --执行存储过程获取output型返回值 
     38 declare  @mysum  int 
     39 execute  PR_Sum 1,2,@mysum  output 
     40 print @mysum 
     41   
     42 --执行存储过程获取Return型返回值 
     43 declare  @mysum2  int 
     44 execute  @mysum2= PR_Sum2 1,2 
     45 print @mysum2--- 
     46  
     47 -------------------自定义函数----------------------- 
     48 --函数的分类: 
     49     1)标量值函数 
     50     2)表值函数 
     51         a:内联表值函数 
     52         b:多语句表值函数 
     53     3)系统函数 
     54  
     55 --新建标量值函数 
     56 create  function  FUNC_Sum1 
     57 ( 
     58      @a  int , 
     59      @b  int 
     60 ) 
     61 returns  int 
     62 as 
     63 begin 
     64      return  @a+@b 
     65 end 
     66   
     67 --新建内联表值函数 
     68 create  function  FUNC_UserTab_1 
     69 ( 
     70      @myId  int 
     71 ) 
     72 returns  table 
     73 as 
     74 return  ( select  *  from  ST_User  where  ID<@myId) 
     75   
     76 --新建多语句表值函数 
     77 create  function  FUNC_UserTab_2 
     78 ( 
     79      @myId  int 
     80 ) 
     81 returns  @t  table 
     82 ( 
     83      [ID] [ int ]  NOT  NULL , 
     84      [Oid] [ int ]  NOT  NULL , 
     85      [Login] [nvarchar](50)  NOT  NULL , 
     86      [Rtx] [nvarchar](4)  NOT  NULL , 
     87      [ Name ] [nvarchar](5)  NOT  NULL , 
     88      [ Password ] [nvarchar]( max )  NULL , 
     89      [State] [nvarchar](8)  NOT  NULL 
     90 ) 
     91 as 
     92 begin 
     93      insert  into  @t  select  *  from  ST_User  where  ID<@myId 
     94      return 
     95 end 
     96   
     97 --调用表值函数 
     98 select  *  from  dbo.FUNC_UserTab_1(15) 
     99 --调用标量值函数 
    100 declare  @s  int 
    101 set  @s=dbo.FUNC_Sum1(100,50) 
    102 print @s 
    103   
    104 --删除标量值函数 
    105 drop  function  FUNC_Sum1 
    106  
    107 -------------------自定义函数与存储过程的区别-------------------- 
    108 --自定义函数: 
    109   1. 可以返回表变量 
    110   2. 限制颇多,包括 
    111     不能使用output参数; 
    112     不能用临时表; 
    113     函数内部的操作不能影响到外部环境; 
    114     不能通过select返回结果集; 
    115     不能update,delete,数据库表; 
    116   3. 必须return 一个标量值或表变量 
    117   自定义函数一般用在复用度高,功能简单单一,争对性强的地方。 
    118 --存储过程 
    119   1. 不能返回表变量 
    120   2. 限制少,可以执行对数据库表的操作,可以返回数据集 
    121   3. 可以return一个标量值,也可以省略return 
    122    存储过程一般用在实现复杂的功能,数据操纵方面。
    123  
  • 相关阅读:
    【poj2396】 Budget
    【bzoj3876】 Ahoi2014—支线剧情
    【uoj207】 共价大爷游长沙
    【bzoj3064】 CPU监控
    【codeforces 103E】 Buying Sets
    【bzoj3938】 Robot
    【bzoj1568】 JSOI2008—Blue Mary开公司
    【hdu5306】 Gorgeous Sequence
    【bzoj2229】 Zjoi2011—最小割
    【bzoj2007】 Noi2010—海拔
  • 原文地址:https://www.cnblogs.com/linjulin/p/5620820.html
Copyright © 2011-2022 走看看