这个存储过程只能执行一次,第二次就没反应,但也不报错
CREATE PROCEDURE yywgspd_xj @djlsh int AS
declare @ZB_DJLSH int,@QZ varchar(10),@ZHDJH varchar(30),@CJWGSPB_DJLSH int,@djbth int,@gzl_djlsh int
declare @ZDRQ datetime,@GSH varchar(10),@ZDR varchar(10),@ZDID varchar(10),@GL bit,@ysdjh varchar(20)
--select @djlsh = 716
--select @ZDRQ=ZDRQ,@GSH=GSH,@ZDR=ZDR,@ZDID=ZDID,@GL=GL from cjwgdjh where djlsh = @djlsh
declare @erro varchar(500)
if exists (select djlsh from t1573h where ysdjh = @djlsh)
begin
SELECT @ERRO='该单据已提交!'
RAISERROR(@Erro,16,-1)
return
end
if exists (select djlsh from cjwgdjs where djlsh = @djlsh and shtg='N' and isnull(ys,'')='')
begin
SELECT @ERRO='审核通过状态为N的必须输入一审人姓名!'
RAISERROR(@Erro,16,-1)
return
end
declare ys_cursor cursor scroll for
select distinct ys,ysid from cjwgdjs where djlsh = @djlsh and ys is not null and shtg='N'
select @djbth=0
Set NOCOUNT ON;
Set XACT_ABORT ON;
begin tran
open ys_cursor
declare @ys varchar(10),@ysid varchar(10)
declare @dylb varchar(20),@cpmc varchar(60),@dh varchar(60),@gskh varchar(60),@wgsl int,@dysqdh varchar(20),
@ywz varchar(10),@sxh decimal,@hh decimal,@cjcs int,@cjyy varchar(10)
while @@fetch_status=0
begin
if @ys is not null
begin
--表头数据
update djbmh set djh=djh+1 where djdm='CJWGSPB'
select @QZ=djQZ,@zhDJH=DJH-1 from DJbmH where DJdM='CJWGSPB'
select @ZHDJH=Rtrim(@ZHDJH)
select @ZHDJH=ISNULL(@QZ,'')+stuff('00000000',9-len(@ZHDJH),len(@ZHDJH),@ZHDJH)
exec @CJWGSPB_DJLSH=sp_GetDjLsh 'CJWGSPB',@CJWGSPB_DJLSH
insert into t1573h (djlsh,djh,nian,yue,gsh,zdr,zdid,zdrq,gl,ys,ysid,ysdjh)
values (@CJWGSPB_DJLSH,@ZHDJH,Year(@ZDRQ),Month(@ZDRQ),@gsh,@zdr,@zdid,@zdrq,@gl,@ys,@ysid,@djlsh)
--表体数据
declare ysmx_cursor cursor scroll for
select dylb,cpmc,dh,gskh,wgsl,dysqdh,ywz,sxh,hh,cjcs,cjyy from cjwgdjs where djlsh=@djlsh and ysid=@ysid
open ysmx_cursor
--declare @dylb varchar(20),@cpmc varchar(60),@dh varchar(60),@gskh varchar(60),@wgsl int,@dysqdh varchar(20),
-- @ywz varchar(10),@sxh decimal,@hh decimal,@cjcs int,@cjyy varchar(10)
while @@fetch_status=0
begin
if @dh is not null and @gskh is not null
begin
select @djbth=@djbth+1
insert into T1573b(djlsh,djbth,dylb,cpmc,dh,gskh,wgsl,dysqdh,ywz,sxh,ydjh,yhhb,cjcs,cjyy)
values(@CJWGSPB_DJLSH,@djbth,@dylb,@cpmc,@dh,@gskh,@wgsl,@dysqdh,@ywz,@sxh,@ZHDJH,@hh,@cjcs,@cjyy)
end
fetch next from ysmx_cursor into @dylb,@cpmc,@dh,@gskh,@wgsl,@dysqdh,@ywz,@sxh,@hh,@cjcs,@cjyy
end
close ysmx_cursor
deallocate ysmx_cursor
end
fetch next from ys_cursor into @ys,@ysid
end
close ys_cursor
deallocate ys_cursor
if @@error<>0
begin
rollback tran
end
else
commit tran
GO
------解决方案--------------------SQL code
CREATE PROCEDURE yywgspd_xj @djlsh INT
AS
DECLARE @ZB_DJLSH INT ,
@QZ VARCHAR(10) ,
@ZHDJH VARCHAR(30) ,
@CJWGSPB_DJLSH INT ,
@djbth INT ,
@gzl_djlsh INT
DECLARE @ZDRQ DATETIME ,
@GSH VARCHAR(10) ,
@ZDR VARCHAR(10) ,
@ZDID VARCHAR(10) ,
@GL BIT ,
@ysdjh V