---
title: "通过一个实际的例子学习SQLServer存储过程"
author: "viwo"
date: "2007-03-30T09:50:04Z"
tags: []
slug: "sqlserver"
draft: false
lastmod: "2007-03-30T09:50:04Z"
id: 66580
url: "https://viwo.iteye.com/blog/66580"
---

前面写过一篇 通过一个实际的例子学习Oracle存储过程，现在再来一篇 通过一个实际的例子学习SQLServer存储过程。  
所谓应用而学。

001. --定义存储过程
002. CREATE PROCEDURE  xxxxxxxx\_p
003. (
004. --传递参数
005. @ym   char(6)
006. )
007. As
008. --定义变量，@xx表示局部变量，@@xx表示全局变量。定义多个变量用","号分割
009. declare @ym\_ln char(6)
010. declare @cpcode char(10),
011. @cpname char(50),
012. @swcode char(10),
013. @swname char(50),
014. @czgscode char(10),
015. @czgscode_ char(10),
016. @czgsname char(50),
017. @qylx char(2),
018. @qyxz char(30)
019. declare @tdcode char(10),
020. @sb\_amt numeric,
021. @sb\_ln\_amt numeric,
022. @sh\_amt numeric,
023. @sh\_ln\_amt numeric,
024. @ts\_amt numeric,
025. @ts\_ln\_amt numeric
026. declare @ybmy numeric(12,6),
027. @jljg numeric(12,6),
028. @other numeric(12,6),
029. @ybmy\_ln numeric(12,6),
030. @jljg\_ln numeric(12,6),
031. @other\_ln numeric(12,6)
032. declare @rowcount int
033. --删除表中现有符合ym=@ym的数据
034. delete from cs\_xxxxxxxx where ym=@ym
035. --给变量赋初值,用到了cast，substring函数。cast用于类型转换，substring用户截取字符串
036. set @ym\_ln=cast((substring(@ym,1,4)-1) as char(4)) +substring(@ym,5,2)
037. --声明一个游标
038. declare cur\_xxxx cursor for
039. select cpcode.code as cpcode ,cpcode.name as cpname ,cpcode.swcode as swcode,swcode.name
040. as swname,cs\_swcode\_czgs.czgs as czgscode,cpcode.qylx as qylx from cpcode
041. left join cs\_swcode\_czgs on cpcode.swcode=cs\_swcode\_czgs.swcode
042. left join swcode on cpcode.swcode=swcode.code where cpcode.swcode&lt;&gt;''
043. --打来游标
044. open cur\_xxxx
045. --取游标中第一行记录并且写入变量。
046. fetch next from cur\_xxxx
047. into  @cpcode,@cpname,@swcode,@swname,@czgscode,@qylx
048. --当@@fetch\_status = 0即取出了有效行时处理，用到了while语句，结构while xx begin xxx end
049. while @@fetch\_status = 0
050. begin
051. --用select语句给变量赋值
052. select @czgscode\_=czgs from cs\_cpcode\_czgs where cpcode=@cpcode
053. --if语句，完整结构if xx begin xxx end
054. if @czgscode_ is not null
055. set @czgscode=@czgscode_
056. select @czgsname=name from cs\_czgs where code=@czgscode
057. --if else结构，完整结构 if xx begin xxx else xxxx end
058. if @qylx='11'
059. set @qyxz='内资企业'
060. else
061. set @qyxz='外商投资企业'
062. --用select语句给变量赋值，用到了isnull函数。
063. select @sb\_amt=isnull(sum(mdtse),0) from mdtsb where sb\_ym=@ym and cpcode=@cpcode
064. select @sb\_ln\_amt=isnull(sum(mdtse),0) from mdtsb where sb\_ym=@ym\_ln and cpcode=@cpcode
065. --
066. --省略n行类似赋值语句
067. --
068. --goto语句,跳转到insertmodule
069. goto insertmodule
070. --
071. --省略n行同类处理语句
072. --
073. insertmodule:
074. --用is null表达式判断是否为null
075. if @sb\_amt      is null set @sb\_amt    =0
076. if @sb\_ln\_amt    is null set @sb\_ln\_amt   =0
077. if @sh\_amt      is null set @sh\_amt    =0
078. if @sh\_ln\_amt    is null set @sh\_ln\_amt   =0
079. if @ts\_amt      is null set @ts\_amt    =0
080. if @ts\_ln\_amt     is null set @ts\_ln\_amt   =0
081. --插入一般贸易
082. select @ybmy=zb from cs\_scqybl where tdcode='一般贸易' and ym=@ym and cpcode=@cpcode
083. select @ybmy\_ln=zb from cs\_scqybl where tdcode='一般贸易' and ym=@ym\_ln and cpcode=@cpcode
084. if @ybmy is null
085. begin
086. --在存储过程中执行存储过程
087. exec xxxbl @cpcode,@ym
088. end
089. if @ybmy\_ln is null
090. begin
091. exec xxxxbl @cpcode,@ym\_ln
092. end
093. select @jljg=zb from cs\_scqybl where tdcode='进料加工' and ym=@ym and cpcode=@cpcode
094. select @jljg\_ln=zb from cs\_scqybl where tdcode='进料加工' and ym=@ym\_ln and cpcode=@cpcode
095. select @other=zb from cs\_scqybl where tdcode='其他' and ym=@ym and cpcode=@cpcode
096. select @other\_ln=zb from cs\_scqybl where tdcode='其他' and ym=@ym\_ln and cpcode=@cpcode
097. --把上面各个步骤运算得到的值insert进表中cs\_xxxxxxxx
098. insert into cs\_xxxxxxxx (xx,xxx,xxxx) values(vv,vvv,vvvv )
099. --取取游标中下一行记录并写入变量
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