使用带有报告参数的top(@parameter)的SSRS导致“无效值”

时间:2018-10-24 16:50:24

标签: sql-server reporting-services

这是在SQL Server 2016和SSRS 2016上。

我有一个数据集,首先使用类似于以下查询的日期来创建日期列表:

declare @StartDate date = '2018-07-01';
declare @EndDate date = '2018-09-30';

if object_id('tempdb..#TempTable') is not null drop table #TempTable;

select top(datediff(d, @StartDate, @EndDate) + 1)
    identity(int, 0, 1) as n
into
    #TempTable
from
    sys.all_columns

当我在数据集查询中明确声明开始日期和结束日期时,此方法很好用。我希望将开始日期和结束日期作为报告参数,因此我删除了声明,并为开始日期(名为ReportParamStartDate)和结束日期(名为ReportParamEndDate)设置了报告参数,两者均设置为Dates,而不是null,我分别给它们提供了默认值'2018-07-01'和'2018-09-30'。参数本身看起来不错,我可以使用简单的select @ReportParamStartDate查询来获取它们并将其显示在报告中。但是,当我将查询修改为以下内容时,出现错误:

if object_id('tempdb..#TempTable') is not null drop table #TempTable;

select top(datediff(d, @ReportParamStartDate, @ReportParamEndDate) + 1)
    identity(int, 0, 1) as n
into
    #TempTable
from
    sys.all_columns

我得到的错误是:

  

TOP或FETCH子句包含无效值。

我尝试进入数据集的Parameters属性,并设置@StartDate = [@ReportParamStartDate]以及结束日期,然后再次修改查询以使用@StartDate和{ {1}}。但这会导致相同的错误。

我想也许我可以通过添加另一个数据集参数来解决这个问题,该参数等于我正在计算的datediff。我创建了一个名为@EndDate的数据集参数,并将其设置为等于@DateDiffParam,然后将查询修改为以下内容:

=DateDiff("d", Parameters!ReportParamStartDate.Value, Parameters!ReportParamEndDate.Value) + 1

这现在给我以下错误:

  

为TOP或FETCH子句的行计数参数提供的行数必须为整数。

因此,我尝试将if object_id('tempdb..#TempTable') is not null drop table #TempTable; select top(@DateDiffParam) identity(int, 0, 1) as n into #TempTable from sys.all_columns 添加到表达式中,但这导致了相同的错误。我进行了一些搜索,发现有人通过执行CInt来解决此错误,但是将select top (1 * @parameter)添加到我的查询中会导致原始错误。

如果我将datediff计算为报告参数(设置为Integer),并在查询中使用它,则还会导致第二个错误,尝试使用此方法的1 *解决方案也将还原为原始错误。

在这一点上,我唯一想到的解决方案是过滤数据集。但是,我想尽可能避免这种情况,因此我希望在使1 * @parameter子句与报表参数一起使用方面有所缺失。

1 个答案:

答案 0 :(得分:0)

我能够创建一个快速报告,该报告同时使用日期和datediff整数作为参数,并使用相同的查询使两者都能正常工作。当参数是文本时,我确实得到了相同的错误,但是当我将其更改为整数时,它起作用了。

这是要检出的报告代码:

<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
  <AutoRefresh>0</AutoRefresh>
  <DataSources>
    <DataSource Name="ODS">
      <DataSourceReference>KHSSQLODSPRD</DataSourceReference>
      <rd:SecurityType>None</rd:SecurityType>
      <rd:DataSourceID>dec28a21-e8a3-4a0c-ac30-1d88f88d8d0f</rd:DataSourceID>
    </DataSource>
  </DataSources>
  <DataSets>
    <DataSet Name="DataSet1">
      <Query>
        <DataSourceName>ODS</DataSourceName>
        <QueryParameters>
          <QueryParameter Name="@ReportParamStartDate">
            <Value>=Parameters!ReportParamStartDate.Value</Value>
          </QueryParameter>
          <QueryParameter Name="@ReportParamEndDate">
            <Value>=Parameters!ReportParamEndDate.Value</Value>
          </QueryParameter>
        </QueryParameters>
        <CommandText>select top(datediff(d, @ReportParamStartDate, @ReportParamEndDate) + 1)
--top(@datediffParam)
    identity(int, 0, 1) as n
into #TempTable
from sys.all_columns

select * from #TempTable</CommandText>
      </Query>
      <Fields>
        <Field Name="n">
          <DataField>n</DataField>
          <rd:TypeName>System.Int32</rd:TypeName>
        </Field>
      </Fields>
    </DataSet>
  </DataSets>
  <ReportSections>
    <ReportSection>
      <Body>
        <ReportItems>
          <Tablix Name="Tablix2">
            <TablixBody>
              <TablixColumns>
                <TablixColumn>
                  <Width>1in</Width>
                </TablixColumn>
              </TablixColumns>
              <TablixRows>
                <TablixRow>
                  <Height>0.25in</Height>
                  <TablixCells>
                    <TablixCell>
                      <CellContents>
                        <Textbox Name="Textbox22">
                          <CanGrow>true</CanGrow>
                          <KeepTogether>true</KeepTogether>
                          <Paragraphs>
                            <Paragraph>
                              <TextRuns>
                                <TextRun>
                                  <Value>n</Value>
                                  <Style>
                                    <FontFamily>Calibri</FontFamily>
                                    <FontWeight>Bold</FontWeight>
                                    <Color>DimGray</Color>
                                  </Style>
                                </TextRun>
                              </TextRuns>
                              <Style>
                                <TextAlign>Center</TextAlign>
                              </Style>
                            </Paragraph>
                          </Paragraphs>
                          <rd:DefaultName>Textbox19</rd:DefaultName>
                          <Style>
                            <Border>
                              <Color>LightGrey</Color>
                              <Style>None</Style>
                            </Border>
                            <BackgroundColor>LightGrey</BackgroundColor>
                            <VerticalAlign>Middle</VerticalAlign>
                            <PaddingLeft>2pt</PaddingLeft>
                            <PaddingRight>2pt</PaddingRight>
                            <PaddingTop>2pt</PaddingTop>
                            <PaddingBottom>2pt</PaddingBottom>
                          </Style>
                        </Textbox>
                      </CellContents>
                    </TablixCell>
                  </TablixCells>
                </TablixRow>
                <TablixRow>
                  <Height>0.25in</Height>
                  <TablixCells>
                    <TablixCell>
                      <CellContents>
                        <Textbox Name="Textbox40">
                          <CanGrow>true</CanGrow>
                          <KeepTogether>true</KeepTogether>
                          <Paragraphs>
                            <Paragraph>
                              <TextRuns>
                                <TextRun>
                                  <Value>=Fields!n.Value</Value>
                                  <Style>
                                    <FontFamily>Calibri</FontFamily>
                                    <Color>DimGray</Color>
                                  </Style>
                                </TextRun>
                              </TextRuns>
                              <Style>
                                <TextAlign>Center</TextAlign>
                              </Style>
                            </Paragraph>
                          </Paragraphs>
                          <rd:DefaultName>Textbox30</rd:DefaultName>
                          <Style>
                            <Border>
                              <Color>LightGrey</Color>
                              <Style>None</Style>
                            </Border>
                            <BackgroundColor>=CODE.AlternateColor("White", "AliceBlue", 1, 0)</BackgroundColor>
                            <VerticalAlign>Middle</VerticalAlign>
                            <PaddingLeft>2pt</PaddingLeft>
                            <PaddingRight>2pt</PaddingRight>
                            <PaddingTop>2pt</PaddingTop>
                            <PaddingBottom>2pt</PaddingBottom>
                          </Style>
                        </Textbox>
                      </CellContents>
                    </TablixCell>
                  </TablixCells>
                </TablixRow>
              </TablixRows>
            </TablixBody>
            <TablixColumnHierarchy>
              <TablixMembers>
                <TablixMember />
              </TablixMembers>
            </TablixColumnHierarchy>
            <TablixRowHierarchy>
              <TablixMembers>
                <TablixMember>
                  <KeepWithGroup>After</KeepWithGroup>
                </TablixMember>
                <TablixMember>
                  <Group Name="Details" />
                </TablixMember>
              </TablixMembers>
            </TablixRowHierarchy>
            <DataSetName>DataSet1</DataSetName>
            <Height>0.5in</Height>
            <Width>1in</Width>
            <Style>
              <Border>
                <Style>None</Style>
              </Border>
            </Style>
          </Tablix>
        </ReportItems>
        <Height>0.5in</Height>
        <Style />
      </Body>
      <Width>8.5in</Width>
      <Page>
        <PageHeight>8.5in</PageHeight>
        <PageWidth>11in</PageWidth>
        <LeftMargin>1in</LeftMargin>
        <RightMargin>1in</RightMargin>
        <TopMargin>1in</TopMargin>
        <BottomMargin>1in</BottomMargin>
        <Style />
      </Page>
    </ReportSection>
  </ReportSections>
  <ReportParameters>
    <ReportParameter Name="ReportParamStartDate">
      <DataType>DateTime</DataType>
      <DefaultValue>
        <Values>
          <Value>1/1/2018 12:00:00 AM</Value>
        </Values>
      </DefaultValue>
      <Prompt>Report Param Start Date</Prompt>
    </ReportParameter>
    <ReportParameter Name="ReportParamEndDate">
      <DataType>DateTime</DataType>
      <DefaultValue>
        <Values>
          <Value>2/1/2018 12:00:00 AM</Value>
        </Values>
      </DefaultValue>
      <Prompt>Report Param End Date</Prompt>
    </ReportParameter>
    <ReportParameter Name="datediffParam">
      <DataType>Integer</DataType>
      <DefaultValue>
        <Values>
          <Value>12</Value>
        </Values>
      </DefaultValue>
      <Prompt>datediff Param</Prompt>
    </ReportParameter>
  </ReportParameters>
  <ReportParametersLayout>
    <GridLayoutDefinition>
      <NumberOfColumns>4</NumberOfColumns>
      <NumberOfRows>2</NumberOfRows>
      <CellDefinitions>
        <CellDefinition>
          <ColumnIndex>0</ColumnIndex>
          <RowIndex>0</RowIndex>
          <ParameterName>ReportParamStartDate</ParameterName>
        </CellDefinition>
        <CellDefinition>
          <ColumnIndex>1</ColumnIndex>
          <RowIndex>0</RowIndex>
          <ParameterName>ReportParamEndDate</ParameterName>
        </CellDefinition>
        <CellDefinition>
          <ColumnIndex>2</ColumnIndex>
          <RowIndex>0</RowIndex>
          <ParameterName>datediffParam</ParameterName>
        </CellDefinition>
      </CellDefinitions>
    </GridLayoutDefinition>
  </ReportParametersLayout>
  <Code>Public bOddRow(10) As Boolean 

Function AlternateColor(ByVal OddColor As String, ByVal EvenColor As String, ByVal Toggle As Boolean, ByVal Type AS INTEGER) As String 

  If Toggle Then bOddRow(Type) = Not bOddRow(Type) 

  If bOddRow(Type) Then 
                Return OddColor 
  Else 
                Return EvenColor 
  End If 

End Function</Code>
  <Language>en-US</Language>
  <ConsumeContainerWhitespace>true</ConsumeContainerWhitespace>
  <rd:ReportUnitType>Inch</rd:ReportUnitType>
  <rd:ReportID>93b48dbc-8a58-48e4-a4a5-2dcdf6700445</rd:ReportID>
</Report>

以下是预览: Preview