这样一个存储过程中的循环,怎么获取不完全。

ah_2056 2013-12-19 08:20:46
ALTER PROCEDURE [dbo].[proc_AdvReportMutSelect]
@VacationDate datetime,
@dateDiff int
AS

declare @i int
set @i=0
WHILE (@i<@dateDiff)
BEGIN

select
b.CIFNm,b.CustomNm,dd.phone,(SUM(isnull(a.RepayAmount-a.receivableAmount,0))) Repaymoney,
kk.PRepayDate,
b.LoanNm,b.LoanStratDate,b.LoanAmount



from

(
select * from InterestReceive
union all select * from InterestReceivetemp
) a
left join
(
select max(convert(varchar(10),PeriodRepyDate,120)) PRepayDate,ContractNm repd from
(
select * from InterestReceive
union all select * from InterestReceivetemp) aa
group by ContractNm
)
kk
on kk.repd=a.ContractNm

left join OPSInf b on a.ContractNm=b.LoanNm
left join (
select CIFNm,phone from
(
select CIFNm,case LoanType when '个人客户' then
case
when SUBSTRING(PerMobliePhB,0,PATINDEX('%|%',PerMobliePhB))='' then PerMobliePhB
else SUBSTRING(PerMobliePhB,0,PATINDEX('%|%',PerMobliePhB))
end
when '企业客户' then CompanyContactMphontA else '' end phone,SalesDate
from LoanInf where Followstate='已审核'
) c

left join
(select MAX(salesdate) salsd ,CIFNm cfin from LoanInf where
Followstate='已审核' group by CIFNm) b

on
b.cfin=c.CIFNm
where salsd = c.SalesDate

) dd

on dd.CIFNm=a.CIFNm

where convert(varchar(10),a.PeriodRepyDate,120)=convert(varchar(10),dateadd(day,@i,@VacationDate),120)
and isnull(a.RepayAmount-a.receivableAmount,0)>0
group by
b.CIFNm,b.CustomNm, dd.phone,kk.PRepayDate,b.LoanNm,b.LoanStratDate,b.LoanAmount
SET @i=@i+1
END

这个存储过程是正确的,但是当我按照红色部分写也是正确的,唯一有问题的地方就是程序读的时候,只能读取到第一条循环记录的值,后面的就读不出来了,奇怪,有人知道么》
比如:
当赋值: @VacationDate ‘2013-12-20’,
@dateDiff 7
这个时候,运行代码,就只能读到1天的数据,但是在SQLSERVER编辑器里测试,就可以读到七天,这个是MSSQLbug么》
...全文
130 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gw6328 2013-12-20
  • 打赏
  • 举报
回复
不是if ,直接把while去掉.
gw6328 2013-12-20
  • 打赏
  • 举报
回复
我觉得大家不要研究他这个局部的错误了. sql像你这样写本身就存在很大的问题, 你为什么要用循环?sql里面不是迫不得已一般不要用循环,游标 你这个完全可以用一句sql就查出来的你去要用循环. 把你的if条件,放到where 里面.我写个大概,你具体修改一下.

--把 if while删掉,把条件改成
WHERE a.PeriodRepyDate>= dateadd(day,0,@VacationDate)
AND a.PeriodREpyDate<dateadd(day,@dateDiff,@VacationDate)
and isnull(a.RepayAmount-a.receivableAmount,0)>0
Leon_He2014 2013-12-20
  • 打赏
  • 举报
回复

if object_id('t') is not null
drop table t;
create table t (id int identity(1,1),edate date)
go
insert into t
select '20131201'
union all select '20131202'
union all select '20131203'
union all select '20131204'
union all select '20131205'
union all select '20131206'
union all select '20131207'
union all select '20131208'
union all select '20131209'
union all select '20131210'
go

CREATE PROCEDURE [dbo].[proc_AdvReportMutSelect]
@VacationDate datetime,
@dateDiff int
AS

declare @i int
set @i=0
WHILE (@i<@dateDiff)
BEGIN

select *
from t
where convert(varchar(10),t.edate,120)=convert(varchar(10),dateadd(day,@i,@VacationDate),120)

SET @i=@i+1

END
go
Exec [proc_AdvReportMutSelect] '20131201',3



我这边循环没问题。
但是这个存储过程返回的是多个结果集。
如果你在其他程序你们执行的话,应该只能获取到最后一个结果集。
建议楼主先把结果放到一个表里面,最后查出来。
我改了一下,如下:

if object_id('t') is not null
drop table t;
create table t (id int identity(1,1),edate date)
go
insert into t
select '20131201'
union all select '20131202'
union all select '20131203'
union all select '20131204'
union all select '20131205'
union all select '20131206'
union all select '20131207'
union all select '20131208'
union all select '20131209'
union all select '20131210'
go

go
CREATE PROCEDURE [dbo].[proc_AdvReportMutSelect]
@VacationDate datetime,
@dateDiff int
AS

declare @t table(id int,edate date)

declare @i int
set @i=0
WHILE (@i<@dateDiff)
BEGIN

insert into @t
select *
from t
where convert(varchar(10),t.edate,120)=convert(varchar(10),dateadd(day,@i,@VacationDate),120)

SET @i=@i+1

END

select *
from @t;
go
Exec [proc_AdvReportMutSelect] '20131201',3
LongRui888 2013-12-20
  • 打赏
  • 举报
回复
帮你改了一下,你再试试:
ALTER PROCEDURE [dbo].[proc_AdvReportMutSelect] 
  @VacationDate datetime,
  @dateDiff int
AS

declare @i int
set @i=0
WHILE (@i<@dateDiff)
BEGIN

select 
b.CIFNm,b.CustomNm,dd.phone,(SUM(isnull(a.RepayAmount-a.receivableAmount,0))) Repaymoney,
kk.PRepayDate,
b.LoanNm,b.LoanStratDate,b.LoanAmount



from 

(
select * from InterestReceive 
union all select * from InterestReceivetemp
 ) a
left join 
(
select  max(convert(varchar(10),PeriodRepyDate,120)) PRepayDate,ContractNm repd from 
(
select * from InterestReceive 
union all select * from InterestReceivetemp) aa
group by ContractNm
)
 kk
on kk.repd=a.ContractNm 

left join OPSInf b on a.ContractNm=b.LoanNm  
left join (
select CIFNm,phone from 
(
select CIFNm,case LoanType when '个人客户' then
case 
when SUBSTRING(PerMobliePhB,0,PATINDEX('%|%',PerMobliePhB))='' then PerMobliePhB
else SUBSTRING(PerMobliePhB,0,PATINDEX('%|%',PerMobliePhB))
end 
when '企业客户' then CompanyContactMphontA else '' end phone,SalesDate 
from LoanInf where Followstate='已审核' 
) c  

left join 
(select MAX(salesdate) salsd ,CIFNm cfin from LoanInf where 
 Followstate='已审核' group by CIFNm) b 
 
 on 
 b.cfin=c.CIFNm 
  where salsd = c.SalesDate
 
  ) dd 
 
 on dd.CIFNm=a.CIFNm 
 
where convert(varchar(10),a.PeriodRepyDate,120)=convert(varchar(10),dateadd(day,@i,@VacationDate),120)
and isnull(a.RepayAmount-a.receivableAmount,0)>0
group by 
b.CIFNm,b.CustomNm, dd.phone,kk.PRepayDate,b.LoanNm,b.LoanStratDate,b.LoanAmount  
SET @i=@i+1

--修改了这里,每次都把@VacationDate 增加1天
set @VacationDate= DATEADD(day,1,@VacationDate)

 END
發糞塗牆 2013-12-20
  • 打赏
  • 举报
回复
单纯在数据库执行能循环吗?
shoppo0505 2013-12-20
  • 打赏
  • 举报
回复
你这个存储过程select只是返回一天的数据,如果程序用要返回多日数据那么要多次运行。 另外 @dateDiff你根本没有没有使用。
ah_2056 2013-12-19
  • 打赏
  • 举报
回复
怎么拼接呢?

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧