作为数据集的一部分添加时,SQL 过程会导致 Visual Studio 挂起

问题描述 投票:0回答:1

我有一个给定参数集的 TSQL 过程返回 34 行,运行时间不到一秒。

sql 有一个 while 循环、一堆嵌套的 if 语句和几种模式。它曾经有一些 goto: 语句,但我将它们更改为 If 语句。

已授予所有适当的访问权限(选择表、执行过程)。

每当我尝试将 SQL 作为过程添加到 SSRS 以构建报告以提取从过程返回的内容时,SSRS 就会挂起。只是挂起。车轮在旋转,似乎永远都在旋转。

我确实运行了测试,并且我能够连接到服务器,我能够在几秒钟内毫无问题地添加不同的过程。但这个问题总是让它崩溃。

首先声明一些标签,主要是日期。 然后创建一些本地表。

然后我们有一个 while 循环读取 while 1=1,我想知道这是否会导致 SSRS 中出现某种无限循环。

then 语法读取如果一个日期值 > 另一个日期值 中断(跳出循环) 别的 在临时表中插入和更新一堆内容。 它确实有开头的语法

这是代码片段:

Parameters 
    @season_str varchar(255),
    @event_start_dt datetime,
    @event_end_dt datetime,
    @sale_end_dt datetime) 

------------------

declare @sale_start_dt datetime
select @sale_start_dt = dateadd(dd,-6,@sale_end_dt)

declare @run_date datetime,
    @include_donated char(1)

--need to report on sales the day before the start date of the report so that we
-- can know how many were sold on day 1 of the report.
select @run_date = dateadd(dd,-1,@sale_start_dt)
select @include_donated = 'N'

create table #t1 (
    perf_no int NOT NULL,
    ...
    )

while 1=1
begin
select @run_date = dateadd(dd,1,@run_date)

if convert(datetime,convert(varchar,@run_date,101)) > convert(datetime,convert(varchar,@sale_end_dt,101))
    BREAK

else 
    begin

    if convert(varchar,@run_date,101) = convert(varchar,getdate(),101)
    --if the run_date is today then we want to get up-to-date numbers so we run the procc with @run_date = NULL
    begin --run_date = NULL

       insert into #t1 ( field names )
       exec LP_SALES_SUMMARY_PTC @event_start_dt, @event_end_dt, @season_str,   NULL, @include_donated
       update #t1 set run_date = @run_date, step = 1 where run_date is null

    end --run_date is not null

    else
    begin --run_date is not NULL
    --if the run date is in the past, run the procedure with @run_date <> NULL
        insert into #t1 ( field names )
        exec LP_SALES_SUMMARY_PTC @event_start_dt, @event_end_dt, @season_str,  NULL, @include_donated
         update #t1 set run_date = @run_date, step = 1 where run_date is null


    if (select count(*) from #t1 where run_date = @run_date) = 0
    begin
        if getdate() >='08/29/2007' -- start of historic information
        begin

        insert into #t1 ( field names )
        exec LP_SALES_SUMMARY_PTC @event_start_dt, @event_end_dt, @season_str,  NULL, @include_donated
        update #t1 set run_date = @run_date, step = 1 where run_date is null
    end

    else
    begin
      
       insert into #t1 ( field names )
       exec RP_SALES_SUMMARY_PTC @event_start_dt, @event_end_dt, @season_str,   NULL, @include_donated
      update #t1 set run_date = @run_date, step = 1 where run_date is null
    end
    end
    end

end

    
-- more code continuation 
sql-server t-sql while-loop reporting-services ssrs-2008-r2
1个回答
0
投票

我不确定对存储过程的调用有什么不同,但我宁愿根据值制定条件,而不是

WHILE 1=1
模式(这是臭名昭著的导致无限循环) 。此外,与其从开始日期减去一天,然后立即在循环内添加一天,不如将开始日期保留原样,直到循环的end 才增加它。

我省略了

#t1
的创建和更新,但以下是我如何重新构造变量和循环:

/* params */
DECLARE @season_str     varchar(255),
        @event_start_dt date,
        @event_end_dt   date,
        @sale_end_dt    date;

/* local variables */
declare @sale_start_dt   date    = dateadd(DAY, -6, @sale_end_dt);
declare @run_date        date    = @sale_start_dt,
        @today           date    = getdate(),
        @beginningOfTime date    = '20070829', -- start of historic info
        @include_donated char(1) = 'N';
...

WHILE @run_date <= @sale_end_dt
BEGIN
  if @run_date = @today 
  begin
    --insert into #t1 (...) EXEC LP_SALES_SUMMARY_PTC with null run_date
  end
  else
  begin
    --insert into #t1 (...) EXEC LP_SALES_SUMMARY_PTC with non-null run_date
  end

  if NOT EXISTS (select 1 from #t1 where run_date = @run_date)
  begin
    if @today >= @beginningOfTime -- start of historic information
    begin
      --insert into #t1 (...) EXEC LP_SALES_SUMMARY_PTC ... -- should it be RP?
    end
    else
    begin -- how could this ever be reached?
      --insert into #t1 (...) EXEC RP_SALES_SUMMARY_PTC ...;
    end
  end
  set @run_date = dateadd(DAY, 1, @run_date);
END
© www.soinside.com 2019 - 2024. All rights reserved.