如何转换此Crystal Report IF语句以在Reporting Services中的WHERE子句中使用?

时间:2010-04-16 00:30:26

标签: reporting-services crystal-reports where-clause

我正在尝试翻译此Crystal Reports IF语句以用于WHERE子句 -

{@receipt_datetime_daylight} in {?DateRange} and
(if {?Call Sign} = "All Call Signs" Then
    {cacs_incident_task.resource_or_class_id} = {cacs_incident_task.resource_or_class_id}
Else If {?Call Sign} = "All Sierra Call Signs" Then
    {cacs_incident_task.resource_or_class_id} in ["S10", "S11", "S12"]
Else If {?Call Sign} = "All Whiskey Call Signs" Then
    {cacs_incident_task.resource_or_class_id} in ["W01", "W02", "W03"]
Else
    {cacs_incident_task.resource_or_class_id} = {?Call Sign}) and
(if {?OffenceType} = "All Offences" Then
    {cacs_inc_type.description} = {cacs_inc_type.description}
else
    {cacs_inc_type.description} = {?OffenceType})

CASE语句在Reporting Services中不起作用,因此我需要找到将其转换为WHERE子句的原因。有谁知道吗?

2 个答案:

答案 0 :(得分:0)

你有三个解决方案:

1-创建一个查询并将这些条件放在其WHERE子句中。

2-为这项工作创建一个expression并使用它来完成这项工作。你可以用BASIC语言书写。

3-您可以使用assembly。这意味着您必须使用自己喜欢的语言制作dll并使用报告中的方法。

答案 1 :(得分:0)

经过一番努力,这是我的解决方案 -

/* Input Parameters */
Declare @StartDate datetime
Declare @EndDate datetime
DECLARE @CallSign varchar(50)
DECLARE @OffenceType varchar(50)

/* Local variables */
declare @CallSignClause nvarchar(1000)
declare @OffenceTypeClause nvarchar(1000)
declare @DateClause nvarchar(1000)
declare @SQLSelect nvarchar(4000)

/* Test Parameters */
Set @CallSign = 'All Whiskey Call Signs'
Set @OffenceType = 'Burglary'
Set @StartDate = { d '2010-01-01' }
Set @EndDate = { d '2010-04-01' }

if @CallSign = 'All Call Signs' OR @CallSign is null
    set @CallSignClause = ' cacs_incident_task.resource_or_class_id = cacs_incident_task.resource_or_class_id'

if @CallSign = 'All Sierra Call Signs' 
    set @CallSignClause = ' (cacs_incident_task.resource_or_class_id IN (''S10'', ''S11'', ''S12'', ''S13'', ''S14'', ''S15'', ''S16'', ''S17'', ''S18'', ''S19'', ''S20'', ''S21'', ''S22'', ''S23'', ''S24'', ''S25'', ''S26'', ''S27'', ''S28'', ''S29'', ''S30'', 
               ''S33'', ''S34'', ''S35'', ''S51'', ''S52'', ''S53'', ''S82'', ''S83''))'

if @CallSign = 'All Whiskey Call Signs' 
    set @CallSignClause = '(cacs_incident_task.resource_or_class_id IN (''W01'', ''W02'', ''W03'', ''W04'', ''W11'', ''W12'', ''W13'', ''W14'', ''W15'', ''W22'', ''W23'', ''W31'', ''W32'', ''W33'', ''W34'', ''W42'', ''W43'', ''W44'', ''W45'', 
               ''W51'', ''W52'', ''W53'', ''W54'', ''W58'', ''W62'', ''W63'', ''W64'', ''W65'', ''W68'', ''W81'', ''W82'', ''W83'', ''W84'', ''W92'', ''W93'', ''W94'', ''W95''))'

if (@CallSign <> 'All Call Signs') AND (@CallSign <> 'All Sierra Call Signs') AND (@CallSign <> 'All Whiskey Call Signs') AND (@CallSign is not null)
    set @CallSignClause = ' cacs_incident_task.resource_or_class_id = ''' + @CallSign + ''''

if @OffenceType = 'All Offences' OR @OffenceType is null
    set @OffenceTypeClause = 'cacs_inc_type.description = cacs_inc_type.description'
else
    set @OffenceTypeClause = 'cacs_inc_type.description LIKE ''%' + @OffenceType + '%'''

if @StartDate is null
    set @DateClause = ''
else
    /* set @DateClause = ' ccors_offence.committed_to_date between cast(''' +left( cast(@StartDate as varchar(20)), 12) + '''  as datetime) and cast(''' +left( cast(@EndDate as varchar(20)), 12) + '''  as datetime)' */
    set @DateClause = ' DateAdd(dd, 0, DateDiff(dd, 0, cacs_incident_header.at_scene_date)) + DateAdd(dd, 0 - DateDiff(dd, 0, cacs_incident_header.at_scene_time), cacs_incident_header.at_scene_time) between cast(''' +left( cast(@StartDate as varchar(20)), 12) + '''  as datetime) and cast(''' +left( cast(@EndDate as varchar(20)), 12) + '''  as datetime) AND ' 

set @SQLSelect = 
'SELECT
    cacs_incident_header."id", cacs_incident_header."receipt_date", cacs_incident_header."receipt_time", cacs_incident_header."receipt_daylight", cacs_incident_header."at_scene_date", cacs_incident_header."at_scene_time", cacs_incident_header."at_scene_daylight",
    cacs_incident_task."resource_or_class_id",
    cacs_resource."description" as cacs_resource,
    cacs_inc_type."description" as cacs_inc_type,
    atscene_datetime = DateAdd(dd, 0, DateDiff(dd, 0, cacs_incident_header.at_scene_date)) + DateAdd(dd, 0 - DateDiff(dd, 0, cacs_incident_header.at_scene_time), cacs_incident_header.at_scene_time) 
FROM
    { oj ((("universe_db"."dbo"."cacs_incident_header" cacs_incident_header INNER JOIN "universe_db"."dbo"."cacs_incident_header_inc_type_id" cacs_incident_header_inc_type_id ON
        cacs_incident_header."id" = cacs_incident_header_inc_type_id."id")
     INNER JOIN "universe_db"."dbo"."cacs_incident_task" cacs_incident_task ON
        cacs_incident_header."id" = cacs_incident_task."incident_header_id")
     INNER JOIN "universe_db"."dbo"."cacs_inc_type" cacs_inc_type ON
        cacs_incident_header_inc_type_id."inc_type_id" = cacs_inc_type."id")
     INNER JOIN "universe_db"."dbo"."cacs_resource" cacs_resource ON
        cacs_incident_task."resource_or_class_id" = cacs_resource."id"}
WHERE ' + @DateClause + @CallSignClause + ' AND ' + @OffenceTypeClause +
' ORDER BY cacs_incident_task."resource_or_class_id" ASC'

/* exec sp_executesql @SQLSelect */
print @SQLSelect