sql-server - SSRS 使用带有报告参数的 top(@parameter) 导致“无效值”
问题描述
这是在 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)设置了报告参数,两者都设置为日期,而不是 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 子句包含无效值。
我尝试进入数据集的参数属性,并设置@StartDate
=[@ReportParamStartDate]
以及结束日期,并再次修改我的查询以使用@StartDate
and @EndDate
。但这会导致相同的错误。
我想也许我可以通过添加另一个等于我正在计算的 datediff 的数据集参数来解决这个问题。我创建了一个名为的数据集参数并将@DateDiffParam
其设置为等于=DateDiff("d", Parameters!ReportParamStartDate.Value, Parameters!ReportParamEndDate.Value) + 1
,然后将查询修改为以下内容:
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
这现在给了我以下错误:
为 TOP 或 FETCH 子句行计数参数提供的行数必须是整数。
所以我尝试添加CInt
到表达式中,但这导致了同样的错误。我搜索了一下,发现有人通过 修复了这个错误select top (1 * @parameter)
,但是在1 *
我的查询中添加了原始错误。
如果我将 datediff 计算为报告参数(设置为整数)并在查询中使用它,这也会导致第二个错误,并且尝试1 * @parameter
此方法的解决方案也会恢复到原始错误。
在这一点上,我能想到的唯一解决方案是过滤数据集。但如果可能的话,我想避免这种情况,所以我希望在让top()
子句与报告参数一起工作方面我缺少一些东西。
解决方案
我能够创建一个快速报告,该报告同时使用日期和 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>
推荐阅读
- mysql - Can I have a trigger that uses different tables argumments?
- php - Login fail - SQLSTATE[28000]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'visitantere'
- reporting-services - 在 DAX 查询中(在 SSRS 数据集中)有条件地分配表变量
- swift - “AppDelegate”类型的值没有成员“appSyncClient” - AWS x iOS
- forms - How can I add an element into form created with formbuilder (Symfony)?
- makefile - put OBJ files in subdirectory in MAKEFILE
- spring - 为什么给定`en_US`时Spring不使用语言环境`en`?
- javascript - Having Javascript logic issues..again
- ios - iOS CoreMotion 态度相对于 iPhone 坐标系
- rest - RestTemplate 返回 null