[此贴子已经被作者于2006-12-12 11:23:15编辑过]
[此贴子已经被作者于2006-12-12 11:23:15编辑过]
这里有个例子,拿去看看吧
CREATE procedure [dbo].[up_GetEquitsAll]
@sctype varchar(10), --存续/上市
@year varchar(4), --年度
@EquitType_id int, --设备类型内码
@builddepart_id int --建设单位内码
as
begin
declare @sql varchar(3000)
--创建输出表
create table #out( apply_id int, --非安设备内码
type_id int, --非安设备类型内码
classname varchar(64), --非安设备类型名称
dept_id int, --建设单位内码
builddeptname varchar(128), --建设单位名称
createdate datetime, --申请日期
name varchar(256), --设备名称
special varchar(256), --规格
unit varchar(16), --单位
num int, --数量
price money, --单价
invest money, --投资
reason varchar(1024), --配备理由
note varchar(1024), --备注
planname varchar(512), --计划名称
yearplantitle varchar(2000) null, --列入哪些年计划
adjustplantitle varchar(2000) null, --列入哪些年调整计划
canDelete bit --是否能删除[根据是否列入年计划,是否列入调整计划] )
--取出所有非安设备信息,插入到临时表中
set @sql = 'insert into #out(apply_id,type_id,classname,dept_id,builddeptname,createdate,name,special,unit,num,price,invest,reason,note)
select a.apply_id,a.type_id,b.classname,a.dept_id,c.name,a.createdate,a.name,a.special,a.unit,a.num,a.price,a.invest,a.reason,a.note
from tequitapply a, dequitclass b, zdumanager..tdepartdefine c
where a.type_id = b.id and a.dept_id = c.depart_id'
--根据@sctype的值进行过滤
set @sql = @sql + (' and sctype = ''' + @sctype +'''')
--根据@year的值进行过滤
if @year <> ''
begin
set @sql = @sql + ' and year(a.createdate) = ' + @year
end
--根据设备类型@equittype_id进行过滤
if @equittype_id <> -1
begin
set @sql = @sql + ' and a.type_id = ' + cast(@equittype_id as varchar(16))
end
if @builddepart_id <> -1
begin
set @sql = @sql + ' and a.dept_id = ' +cast(@builddepart_id as varchar(16))
end
--执行查询
exec(@sql)
*******************************************************************************************************************************************
--使用游标获取一个非安设备申请列入哪些年计划的名称
declare @apply_id int, @count int, @index int, @yearPlanTitle varchar(2000),@adjustPlanTitle varchar(2000), @canmake bit --是否可以删除
set @yearPlanTitle = ''
set @adjustPlanTitle = ''
set @count = 0
set @index = 1
declare title cursor for select apply_id from #out
open title
fetch next from title into @apply_id
while @@fetch_status = 0
begin
------------------------------------获取列入年计划名称字符串------------------------------------------------------------------------------------------------------
--创建临时表-(列入年计划的计划名称)
create table #planName(id int IDENTITY(1,1), planname varchar(512))--存放查询出来的计划名称结果集
insert into #planName(planname)
SELECT a.planname FROM tInvestPlan a, tPlanEquitR b, dbo.tEquitApply c
where a.plan_id = b.plan_id and b.apply_id = c.apply_id and c.apply_id = @apply_id order by a.createdate asc
--存储从临时表里面查出来的单个planname
declare @pName varchar(512)
--取出最大id[多少条记录]
select @count = max(id) from #planName
--循环处理表生成字符串
while (@index -1) < @count
begin
select @pName = planname from #planName where id = @index
set @yearPlanTitle = @yearPlanTitle + '<br>' + cast(@index as varchar(16)) + '、' + @pName
set @index = @index + 1
end
drop table #planName --删除临时表
---------------------------------------获取列入调整计划名称字符串----------------------------------------------------------------------------------------------------
set @count = 0
set @index = 1
--创建临时表-(列入调整计划的计划名称)
create table #adjustName(id int IDENTITY(1,1), planname varchar(512))--存放查询出来的计划名称结果集
insert into #adjustName(planname)
SELECT a.planname FROM tAdjustInvestPlan a, tAdjustPlanEquitR b, tEquitApply c
where a.plan_id = b.plan_id and b.apply_id = c.apply_id and c.apply_id = @apply_id order by a.createdate asc
--存储从临时表里面查出来的单个planname
declare @aName varchar(512)
--取出最大id[多少条记录]
select @count = max(id) from #adjustName
--循环处理表生成字符串
while (@index -1) < @count
begin
select @aName = planname from #adjustName where id = @index
set @adjustPlanTitle = @adjustPlanTitle + '\n' + cast(@index as varchar(16)) + '、' + @aName
set @index = @index + 1
end
drop table #adjustName --删除临时表
------------------------------------------------获取删除字段---------------------------------------------------------------------------------------------------------------
--获取列值[canmake = 0 已下达][列入年计划]
SELECT @canmake = a.canmake FROM tInvestPlan a, tPlanEquitR b, tEquitApply c
where a.plan_id = b.plan_id and b.apply_id = c.apply_id and c.apply_id = @apply_id
--获取列值[canmake = 0 已下达][列入年调整计划]
if @canmake <> 0
begin
select @canmake = a.canmake from tAdjustInvestPlan a, tAdjustPlanEquitR b, tEquitApply c
where a.plan_id = b.plan_id and b.apply_id = c.apply_id and c.apply_id = @apply_id
end
--更新表[#out]字段[年计划名称yearplantitle],[年调整计划名称字符串adjustplantitle],[是否能删除canDelete]-------------------------
update #out set yearplantitle = @yearPlanTitle, adjustPlanTitle = @adjustPlanTitle, canDelete = @canmake
where apply_id = @apply_id
fetch next from title into @apply_id
end
close title
deallocate title