sqlserver函数,存储过程,游标以及while和if语句的嵌套使用
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
sqlserver函数,存储过程,游标以及while和if语句的嵌套使⽤编写这个存储过程的主要原因是⾃⼰太懒,不愿意每天往表⾥⾯做数据,所以就写了他。
不仅仅是学习,也是⾃⼰的锻炼。
USE [oaerp]
GO
/****** Object: StoredProcedure [dbo].[s_Get_AutoYh] Script Date: 02/11/2015 17:17:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,ljq>
-- Create date: <Create Date,,2015-2-1>
-- Description: <Description,,⾃动⽣成⼀个门店指定时间范围的所有需要养护商品的养护记录>
-- =============================================
ALTER PROCEDURE [dbo].[s_Get_AutoYh]
@sdtm datetime=null,--开始的时间
@edtm datetime=null,--结束的时间
@DepoID char(10)=null,
@UserID char(20)=null,
@type varchar(30)=null,
-- 影响的⾏数
@num int=null output
AS
--⼀般药品养护记录(ybyh)
if (@type='ybyh')
BEGIN
--创建⼀张临时表
select a.* into #tmpyh from dbo.GetGoodsBatchListUnReal(@DepoID,'') a inner join ggoods b on a.goodsid=b.goodsid
left join gGoodsExt c on b.goodsid =c.goodsid
where a.amount >0
and b.catid like '01%'
and c.storeid !='07'
order by a.effdate
declare @days int --计算天数
declare @count int --统计要养护的数据
declare @Idx int
declare @n int --记录每天需要插⼊的数据
declare @m int --记录执⾏的天数
declare @Doccode varchar(30)
declare @GoodsID char(10)
declare @Amount decimal(18,6)
declare @BatchNo varchar(40)
declare @EffDate datetime
declare @StockArea char(2)
declare @Station char(2)
declare @CheckResult char(2)
declare @DoMethod char(2)
declare @MakID char(10)
declare @MakDtm datetime
declare @MakDtm datetime
-- 开始和结束时间的差就是本次需要添加养护记录天数
set @days = datediff(day,@sdtm,@edtm)
if @days = 0
begin
set @days = 1
end
--得到总的记录数,即当前店所有的品种
select @count = count(*) from #tmpyh
--设置每天的记录条数,向下取整。
set @n=floor(@count/30)
set @m=1
--开始事务
--begin tran
begin try
set @makdtm
while(@days>0)
begin
declare ybyh cursor global-- 定义⼀个游标
read_only
for select goodsid,batchno,effdate,amount from #tmpyh
open ybyh
----如果执⾏的天数在输⼊的时间范围内
--while(@m<)
--begin
-- --每执⾏三⼗天,结束⼀次循环。
-- if(@m%30!=0)
-- BEGIN
-- 打开游标
--给参数赋值@goodsid,@batchno,@effdate,@amount
fetch next from ybyh into @goodsid,@batchno,@effdate,@amount
--执⾏游标第⼀条记录
--=fetch next from ybyh
exec [s_Get_DocCodeSerial_ByTradeID] @TradeID='60',@depoID=@DepoID,@DocCode=@DocCode output
while (@@fetch_status = 0)
BEGIN
--执⾏每天需要插⼊的数据,当@n⼩于零的时候跳出循环
--while(@n>0)
--begin
--if not exists(select * from zGspyhRecord where goodsid and isnull(batchno,'')=isnull(@batchno,'') and isnull(effdate,'') =isnull(@effdate,''))
--begin
select @stockarea = case a.otcid
when '01' then '00'
when '01' then '00'
when '02' then '01'
when '03' then '02'
end
from ggoods a where goodsid
select @Idx=isnull(max(Idx),0)+1 from zGspyhRecord where
--设置时间和养护员信息
set @MakID=@UserID
--select DATEADD(dd,5550,'2015-02-12 11:11:13')
insert into zGspyhRecord(DocCode, DepoID, Idx, GoodsID, Amount,BatchNo, EffDate, StockArea, Station, CheckResult, DoMethod, MakID, MakDtm, CheID, CheDtm)
values(@DocCode,@DepoID,@Idx,@GoodsID,@Amount,@BatchNo,@EffDate,@stockarea,'00','00','03',@MakID,@MakDtm,null,nu
set @n=@n-1
--在秒上⾯加上idx序号,作为新的时间的秒
set @Idx=@Idx+1
set @MakDtm=DATEADD(SS,@Idx,@MakDtm)
print('today had '+str(@n) )
if @n<=0
begin
exec [s_Get_DocCodeSerial_ByTradeID] @TradeID='60',@depoID=@DepoID,@DocCode=@DocCode output
set @MakDtm=DATEADD(DD,1,@MakDtm)
set @days -1
set @n=floor(@count/30)
if(@days%30=0)
begin
print('days:'+str(@days))
break
end
end
--end
-- 执⾏游标,获得数据,直到读取所有的数据集合
fetch next from ybyh into @goodsid,@batchno,@effdate,@amount
--end
--END
--set @m=@m+1 --设置天数增加1天
--set @MakDtm=DATEADD(DD,1,@MakDtm)
END
close ybyh -- 关闭游标
deallocate ybyh -- 删除游标
end
--返回执⾏的记录,如果⼤于0,表⽰执⾏成功 set @num
select @num
--commit tran
--结束事务
end try
begin catch
print error_message()
--rollback tran
end catch
END。