Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用一个过程运行一个过程两次_Sql_Sql Server 2008 - Fatal编程技术网

Sql 使用一个过程运行一个过程两次

Sql 使用一个过程运行一个过程两次,sql,sql-server-2008,Sql,Sql Server 2008,我有一个从SSRS报告运行的存储过程,我现在的问题是我需要在报告中包含上一年的数据。我希望能够使用一组参数运行一次报告,而不是第二次使用前几年的数据运行报告,以便能够逐行比较数据。我所做的是制造一个错误。我对SQL Server有些陌生,非常感谢您的帮助。这是内置于SQL Server 2008中的 DECLARE @StartDate datetime, @EndDate datetime, @iClientID int, @iServiceLevelID int

我有一个从SSRS报告运行的存储过程,我现在的问题是我需要在报告中包含上一年的数据。我希望能够使用一组参数运行一次报告,而不是第二次使用前几年的数据运行报告,以便能够逐行比较数据。我所做的是制造一个错误。我对SQL Server有些陌生,非常感谢您的帮助。这是内置于SQL Server 2008中的

DECLARE 
   @StartDate datetime,
   @EndDate datetime,
   @iClientID int,
   @iServiceLevelID int

SET @StartDate = '1-1-13'
SET @EndDate = '12-30-13'
SET @iClientID = null

SET DATEFIRST 7

DECLARE @DATA table(iclientID int,
                    sClientCode varchar(8),
                    sClientName varchar(50),
                    sServiceLevelName varchar(50), 
                    DeailyProductionAverage float, 
                    CorrectionPercentage  float, 
                    AverageAging float, 
                    decProduction float, 
                    EffectedDate datetetime, 
                    RepID int,
                    FirstName varchar(50), 
                    LastName varchar(50), 
                    Completed float)

insert into @DATA
exec procSSRS_ClientPerformanceNew_2  @StartDate, 
                                      @EndDate, 
                                      @iClientID, 
                                      @iServiceLevelID

insert into @DATA
exec procSSRS_ClientPerformanceNew_2 dateadd(year, -1, @StartDate)
                                     dateadd(year, -1, @Enddate)
                                     @iClientID
                                     @iServiceLevelID  

第二次调用过程时,参数列表中缺少逗号。将您的代码更改为此以使其正常工作:

exec procSSRS_ClientPerformanceNew_2

dateadd(year,-1,@StartDate),
dateadd(year,-1,@Enddate),
@iClientID,
@iServiceLevelID  

在SQL Server中,向存储过程传递参数时,该参数可以是常量或变量。例如,它不能是数学表达式,也不能是函数调用

因此,为了使用不同的数据范围再次调用该过程,您需要存储
dateadd
调用变量的结果,并将其用作参数。例如,如果以后不需要它们的原始值,您可以重新使用
@StartDate
@EndDate

...

set @StartDate = dateadd(year,-1,@StartDate);
set @EndDate = dateadd(year,-1,@Enddate);

insert into @DATA
exec procSSRS_ClientPerformanceNew_2  @StartDate, 
                                      @EndDate, 
                                      @iClientID, 
                                      @iServiceLevelID
;

或者您可以声明另外两个
datetime
变量。

年附近的语法不正确。我建议您在输入日期文字时使用ISO 8601格式
'YYYY-MM-DD'
。无论您的DB设置是什么,都会正确解释这一点。鉴于“1-1-13”尚未立即明确;它可能是
Y-M-D
(2001年1月13日)或
D-M-Y
(2013年1月1日)