通过一个实际的例子学习SQLServer存储过程

  1. --定义存储过程   
  2. CREATE PROCEDURE  xxxxxxxx_p   
  3.  (   
  4.   --传递参数   
  5.   @ym   char(6)   
  6.  )   
  7. As  
  8.   --定义变量,@xx表示局部变量,@@xx表示全局变量。定义多个变量用","号分割   
  9.  declare @ym_ln char(6)   
  10.  declare @cpcode char(10),   
  11.   @cpname char(50),   
  12.   @swcode char(10),   
  13.   @swname char(50),   
  14.   @czgscode char(10),   
  15.   @czgscode_ char(10),   
  16.   @czgsname char(50),   
  17.   @qylx char(2),   
  18.   @qyxz char(30)   
  19.  declare @tdcode char(10),   
  20.   @sb_amt numeric,   
  21.   @sb_ln_amt numeric,   
  22.   @sh_amt numeric,   
  23.   @sh_ln_amt numeric,   
  24.   @ts_amt numeric,   
  25.   @ts_ln_amt numeric  
  26.  declare @ybmy numeric(12,6),   
  27.   @jljg numeric(12,6),   
  28.   @other numeric(12,6),   
  29.   @ybmy_ln numeric(12,6),   
  30.   @jljg_ln numeric(12,6),   
  31.   @other_ln numeric(12,6)   
  32.  declare @rowcount int  
  33.  --删除表中现有符合ym=@ym的数据   
  34.  delete from cs_xxxxxxxx where ym=@ym   
  35.   --给变量赋初值,用到了cast,substring函数。cast用于类型转换,substring用户截取字符串   
  36.  set @ym_ln=cast((substring(@ym,1,4)-1) as char(4)) +substring(@ym,5,2)   
  37.  --声明一个游标   
  38.  declare cur_xxxx cursor for    
  39.   select cpcode.code as cpcode ,cpcode.name as cpname ,cpcode.swcode as swcode,swcode.name  
  40.   as swname,cs_swcode_czgs.czgs as czgscode,cpcode.qylx as qylx from cpcode    
  41.   left join cs_swcode_czgs on cpcode.swcode=cs_swcode_czgs.swcode    
  42.   left join swcode on cpcode.swcode=swcode.code where cpcode.swcode<>''    
  43.  --打来游标   
  44.  open cur_xxxx   
  45.  --取游标中第一行记录并且写入变量。   
  46.  fetch next from cur_xxxx    
  47.   into  @cpcode,@cpname,@swcode,@swname,@czgscode,@qylx   
  48.  --当@@fetch_status = 0即取出了有效行时处理,用到了while语句,结构while xx begin xxx end   
  49.  while @@fetch_status = 0   
  50.  begin  
  51.   --用select语句给变量赋值   
  52.   select @czgscode_=czgs from cs_cpcode_czgs where cpcode=@cpcode   
  53.   --if语句,完整结构if xx begin xxx end   
  54.   if @czgscode_ is not null  
  55.    set @czgscode=@czgscode_   
  56.   select @czgsname=name from cs_czgs where code=@czgscode   
  57.   --if else结构,完整结构 if xx begin xxx else xxxx end   
  58.   if @qylx='11'   
  59.    set @qyxz='内资企业'   
  60.   else  
  61.    set @qyxz='外商投资企业'   
  62.   --用select语句给变量赋值,用到了isnull函数。   
  63.   select @sb_amt=isnull(sum(mdtse),0) from mdtsb where sb_ym=@ym and cpcode=@cpcode   
  64.   select @sb_ln_amt=isnull(sum(mdtse),0) from mdtsb where sb_ym=@ym_ln and cpcode=@cpcode   
  65.   --   
  66.   --省略n行类似赋值语句   
  67.   --   
  68.   --goto语句,跳转到insertmodule   
  69.   goto insertmodule   
  70.    --   
  71.   --省略n行同类处理语句   
  72.   --   
  73. insertmodule:   
  74.   --用is null表达式判断是否为null   
  75.   if @sb_amt      is null set @sb_amt    =0           
  76.   if @sb_ln_amt    is null set @sb_ln_amt   =0              
  77.   if @sh_amt      is null set @sh_amt    =0           
  78.   if @sh_ln_amt    is null set @sh_ln_amt   =0              
  79.   if @ts_amt      is null set @ts_amt    =0           
  80.   if @ts_ln_amt     is null set @ts_ln_amt   =0     
  81.   --插入一般贸易   
  82.     select @ybmy=zb from cs_scqybl where tdcode='一般贸易' and ym=@ym and cpcode=@cpcode   
  83.     select @ybmy_ln=zb from cs_scqybl where tdcode='一般贸易' and ym=@ym_ln and cpcode=@cpcode   
  84.     if @ybmy is null  
  85.      begin  
  86.       --在存储过程中执行存储过程   
  87.       exec xxxbl @cpcode,@ym   
  88.      end  
  89.     if @ybmy_ln is null  
  90.      begin  
  91.       exec xxxxbl @cpcode,@ym_ln   
  92.      end  
  93.     select @jljg=zb from cs_scqybl where tdcode='进料加工' and ym=@ym and cpcode=@cpcode   
  94.     select @jljg_ln=zb from cs_scqybl where tdcode='进料加工' and ym=@ym_ln and cpcode=@cpcode   
  95.     select @other=zb from cs_scqybl where tdcode='其他' and ym=@ym and cpcode=@cpcode   
  96.     select @other_ln=zb from cs_scqybl where tdcode='其他' and ym=@ym_ln and cpcode=@cpcode   
  97.   --把上面各个步骤运算得到的值insert进表中cs_xxxxxxxx   
  98.     insert into cs_xxxxxxxx (xx,xxx,xxxx) values(vv,vvv,vvvv )   
  99.   --取取游标中下一行记录并写入变量   
  100.   fetch next from cur_xxxx into  @cpcode,@cpname,@swcode,@swname,@czgscode,@qylx   
  101.  --结束while循环   
  102.  end  
  103.  --关闭游标   
  104.  close cur_xxxx   
  105.  --删除游标   
  106.  deallocate cur_xxxx   
  107. GO   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行。 存储过程中可以包含逻辑控制语句和数据操纵语句,它可以接受参数、输出参数、返回单个或多个结果集以及返回值。 由于存储过程在创建时即在数据库服务器上进行了编译并存储在数据库中,所以存储过程运行要比单个的SQL语句块要快。同时由于在调用时只需用提供存储过程名和必要的参数信息,所以在一定程度上也可以减少网络流量、简单网络负担。 1、 存储过程的优点 A、 存储过程允许标准组件式编程 存储过程创建后可以在程序中被多次调用执行,而不必重新编写该存储过程的SQL语句。而且数据库专业人员可以随时对存储过程进行修改,但对应用程序源代码却毫无影响,从而极大的提高了程序的可移植性。 B、 存储过程能够实现较快的执行速度 如果某一操作包含大量的T-SQL语句代码,分别被多次执行,那么存储过程要比批处理的执行速度快得多。因为存储过程是预编译的,在首次运行一个存储过程时,查询优化器对其进行分析、优化,并给出最终被存在系统表中的存储计划。而批处理的T-SQL语句每次运行都需要预编译和优化,所以速度就要慢一些。 C、 存储过程减轻网络流量 对于同一个针对数据库对象的操作,如果这一操作所涉及到的T-SQL语句被组织成一存储过程,那么当在客户机上调用该存储过程时,网络中传递的只是该调用语句,否则将会是多条SQL语句。从而减轻了网络流量,降低了网络负载。 D、 存储过程可被作为一种安全机制来充分利用 系统管理员可以对执行的某一个存储过程进行权限限制,从而能够实现对某些数据访问的限制,避免非授权用户对数据的访问,保证数据的安全。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值