/****** 对象: 存储过程 dbo.get_zb_count 脚本日期: 2007-05-18 15:34:33 ******/
CREATE PROCEDURE get_zb_count_3
@J_JID varchar(50),
@S_Area varchar(6000),
@K_ID varchar(50),
@zb_count int output
AS
begin
declare @i int ---存放循环变量
declare @count int ---存放表的数据记录数
declare @Kh_ID varchar(50)-----存放考核子指标值
declare @total int ------存放count的循环累计
create table #KhStyle(IntID int identity(1,1), StyleID varchar(50)) ---创建临时表
insert into #KhStyle(StyleID) select K_ID from T_Kaoh where J_JID=@J_JID and K_FID=@K_ID --------把查询的子指标记录插入到临时表
select @count = count(1) from #KhStyle ---查询当前临时表的子指标记录
if (@count>0)
-------------------------------------------------1----------------------------------
begin
set @i = 1 -------变量初始化
set @total = 0 ---累计初始化
while (@i <= @count) -------开始循环
--------------------------------------------2--------------------------
begin
select @Kh_ID = StyleID from #KhStyle where intID = @i --循环把临时表的子指标值 给于变量 @Kh_ID
select @zb_count=count(*) from v_kh_jg where J_ID=@J_JID and K_ID=@Kh_ID and S_Area in (select * from GetTB(@S_Area)) -----当前子指标所属企业数
if @zb_count = 0
select @zb_count = 1 ---如果没有企业则为1
select @total= @zb_count +@total ---累计
select @i = @i +1 --循环递增
end
-------------------------------------------2---------------------------
set @zb_count=@total
end
---------------------------------------------1---------------------------------
else
set @zb_count=0
drop table #KhStyle---删除临时表
end
GO
说明:我这里用到了临时表的功能,通过这个存储过程至少能学到三点知识
1、变量是如何定义的
以下为程序代码:
declare @intID int declare @varname varchar(100) |
Create Table #StyleTab(intID int identity(1,1),StyleID varchar(50))----创建临时表 请注意格式:#表名(字段名称 类型,字段名称 类型) 比较有用的一个字段就是自动增长的标识性字段的定义 intID int identity(1,1) Drop Table #StyleTab----删除临时表 |
一般i通常作为变量 while(i<=5) begin -----代码 end |
================================self============================================
ALTER PROCEDURE ake_viewParkinf
@proc_inf nvarchar(50) output
AS
begin
declare @i int --循环变量
declare @parkofNum int --车库数目,也是循环的次数
select @parkofNum= (select count(*) from parkOf) --取得车库数
Create Table #tempTable(tempID int identity(1,1),parkofNo varchar(10),parkNum int,voidparkNum int) --创建临时表
---------------------
if (@parkofNum >0)
begin
set @i = 1
while(@i <= @parkofNum)
begin
declare @tempParkNum int
declare @tempvoidParkNum int
set @tempParkNum = (select count(*) from parkinf where parkof=@i) --编号为@i的车库的车位数量
set @tempvoidParkNum = (select count(*) from parkinf where parkof=@i and parkstate = 'void') -- 编号为@i的车库的停放车辆的车位数量
insert into #tempTable(parkofNo,parkNum,voidparkNum) values(@i,@tempParkNum,@tempvoidParkNum) --将编号,总车位数和停放车位数插入临时表
set @i = @i + 1 --循环变量自增
end
select parkof,parkofName,parkNum,voidparkNum from #tempTable,parkOf where #tempTable.parkofNo=parkOf.parkof --查询的最终结果
set @proc_inf ='success'
end
else
begin
drop table #tempTable --删除临时表
set @proc_inf ='当前没有车库车位信息,请添加车库后查询!'
end
end