Perform Subquery in SSRS with Parameterized Query

devIt04

Summary: I have a query that I would like to parameterize for user specified tables and user specified dates in SSRS. The query includes several CTE's. How do I use the paramertized query as a CTE?

I have been able to paramertize the "selectedData" and it works like this:

declare @cmd nvarchar(max)
Select @cmd = ' select  [id] 
      ,[theTime]
      ,[theDate]
      ,[KW]
      ,[airspeed_mph]
     , [avg_drive_speed]
     ,Lag([avg_drive_speed]) over (order by id) previousValue
From [tunnelData].[dbo].['  + @tunnel + ']
where [theDate]>=(' + @date + ' )'
exec (@cmd)

Now I want to add this as a CTE to reduce the data further in the query. The following code was my attempt but I get an error from the Query Designer in SSRS "incorrect syntax near 'declare'"

-- First the Paramerized Table

WITH selectedData as (
declare @cmd nvarchar(max)

Select @cmd = ' select  [id] 
      ,[theTime]
      ,[theDate]
      ,[KW]
      ,[airspeed_mph]
      ,[avg_drive_speed]
     ,Lag([avg_drive_speed]) over (order by id) previousValue
From [tunnelData].[dbo].['  + @tunnel + ']
where [theDate]>=(' + @date + ' )'
exec (@cmd)  ),


-- Second Table to Reduce Data

lagData as (
    SELECT *,
        Lag([theTime]) over (order by id) previousTime
    from selectedData
    where [avg_drive_speed] >= 40 and previousValue < 40 OR [avg_drive_speed] < 40 and previousValue >= 40
    ) 

-- Last Select to Calculate Difference

Select *,
Case 
    when avg_drive_speed >= 40
    then 0 
    else datediff(SECOND, previousTime, [theTime]) 
end AS runTime

from lagData
order by [theDate] ASC, [theTime] ASC

I can run the CTE without the parameterization and it works:

With selectedData as (
    Select
       [id]
      ,[theTime]
      ,[theDate]
      ,[KW]
      ,[airspeed_mph]
      ,[avg_drive_speed]
      ,Lag([avg_drive_speed]) over (order by id) previousValue

      FROM [tunnelData].[dbo].[aus]                                             
      where [theDate]>='2019-08-07' and [theDate]<= '2019-08-07'
      ),

lagData as (
    SELECT *,
        Lag([theTime]) over (order by id) previousTime
    from selectedData
    where [avg_drive_speed] >= 40 and previousValue < 40 OR [avg_drive_speed] < 40 and previousValue >= 40
    ) 

Select *,
Case 
    when avg_drive_speed >= 40
    then 0 
    else datediff(SECOND, previousTime, [theTime]) 
end AS runTime

from lagData
order by [theDate] ASC, [theTime] ASC

Here are the results I get if I remove the paramertization (@tunnel / @date) and run the code immediately above this:

Output Table Image from SSMS

pwilcox

Well, you would place the entire query in the dynamic variable:

declare @cmd nvarchar(max) = '

    with 

        selectedData as (

            select     ....
            from       [tunnelData].[dbo].[' + @tunnel +']                                             
            where      [theDate] >= ''' + @date + '''

        ),

        lagData as (...) 

        select      ...
        from        lagData
        order by    [theDate] ASC, [theTime] ASC

';

exec (@cmd);

Fill in the ellipsis accordingly.

But be very careful. Your code is very prone to sql injection via the @tunnel and @date variables.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related