Firedac - 更改SQL语句以通过cte返回不同​​的结果集

时间:2017-12-18 22:58:18

标签: sql delphi firedac

我的问题是在运行时(Delphi10 vcl)我的fdquery只返回我在第一次运行中使用的结构。尝试发布,打开,关闭,空视图等...问题:从我的fdquery组件的初始运行中清除列和结构需要什么,以便相同的实例可以在后续运行中返回不同的数据集? (参见以下部分 - 这是我的问题)

    declare @ctes varchar(max),
        @sql1 varchar(max),
        @sql2 varchar(max),
        @sql3 varchar(max),
        @sqltemp1 varchar(max),
        @sqltemp2 varchar(max),
    @dbyr varchar(25),
    @dbyrstu varchar(25),
    @dbyrads varchar(25),
    @dbyrdsp varchar(25),
        @yr1  int,
        @Startyr  int,
        @yr2  int,
        @Endyr int,
    @x int

-- KETools (uncomment)
set @startyr = :parmStartYear
set @Endyr   = :parmEndYear
set @x       = :parmX

-- SQL uncomment to test)
-- set @startyr = 2017
-- set @Endyr   = 2016
-- set @x = 4

set @yr1 = @startyr
set @yr2 = @Endyr

set @dbyrstu = 'DST'+cast(@yr1-2000 as varchar(5))+'000CUSD..stu'
set @dbyrads = 'DST'+cast(@yr1-2000 as varchar(5))+'000CUSD..ads'
set @dbyrdsp = 'DST'+cast(@yr1-2000 as varchar(5))+'000CUSD..dsp'


while @yr1 >= @yr2 
begin
     set @ctes = '
        with cteSusData
        as  (
            select  s.id,s.sc, l.nm [School - '+cast(@yr1 as varchar(5))+'] , s.ln+'', ''+s.fn [Student Name]
            , isnull(case
               when s.ec = 500 then ''Hispanic''
               when s.ec = 600 then (select DE from cod where tc = ''STU'' and FC = ''RC1'' and cd = s.rc1)
              end,'''') Ethnicity
            , isnull(case
                when s.sp = ''S'' then ''SDC''
                when s.u6 = ''R'' then ''RSP''
                when s.u6 = ''D'' then ''DIS''
                when (c.ed is not null and (c.xd is null or c.xd = '''')) Then ''SDC*''
              end,''REG'') EdTYPE
            , s.de [Days Enrolled]
            , s.dp [Days Present]
            , s.da [Days Absent]
            , d.dy [number of Days suspended]
            , d.ds [TYPE]
            , convert(varchar(10), d.dd,101) [Date Suspended]
            ,case
               when month(d.dd) between 8 and 12 then year(d.dd)
               when month(d.dd) between 1 and 7 then year(d.dd)-1
             end [SchoolYear]

            from '+@dbyrstu+' s inner join '+@dbyrads+' a
            on s.id = a.pid and s.sc = a.scl
            inner join '+@dbyrdsp+' d 
            on a.pid = d.pid and a.sq = d.sq 
            inner join loc l
            on s.sc = l.cd
            left join cse c
            on s.id = c.id
            where s.sc between 29 and 72
            and d.ds like ''%SUS%''
            )
        , cteSummarySusDateEthnicity
        as (
           select distinct sc,[School - '+cast(@yr1 as varchar(5))+'] ,Ethnicity
           ,count(*) over (partition by sc,ethnicity) [Ethnicity Suspensions]
           ,EdTYPE
           ,count(*) over (partition by sc,ethnicity,EdTYPE) [EdType Suspensions]
           ,count(*) over (partition by sc) [Total School Suspensions]
           from cteSusData
           where SchoolYear = '+cast(@yr1 as varchar(5))+'
           )
        , cteSummarySusSchool
        as (
           select distinct sc
                 ,[School - '+cast(@yr1 as varchar(5))+'] 
                 ,sum([Ethnicity Suspensions])over (partition by sc) [Total Suspension - '+cast(@yr1 as varchar(5))+']
                 from cteSummarySusDateEthnicity
           )'
    --------- Full run
    set @sql1 = @ctes + ' select * from cteSusData'
                      + ' where  SchoolYear = '+cast(@yr1 as varchar(5))+'' 
                      + ' order by cteSusData.sc,cteSusData.[Student Name] '  --,cteSusData.[School of Suspension]
    --------- Summary by ethnicity
    set @sql2 = @ctes + ' select  * from cteSummarySusDateEthnicity '
    --------- Summary by School
    set @sql3 = @ctes + ' select * from cteSummarySusSchool '

    --------- Make temp files for final combine 
    set @sqltemp1 = replace(@sql3,'select * from cteSummarySusSchool','select * into amc..tempke'+cast(@yr1 as varchar(5))+' from cteSummarySusSchool')

--------------------------------------------------------------------------------------------------------------------------------------
----  This is my question ???? If I set the parmeter X in my calling code how do you clear the FDquery so the next call returns the next dataset
----  Works fine at design time but not runtime
--------------------------------------------------------------------------------------------------------------------------------------

    if @x = 1  execute (@sql1) -- Full run
    if @x = 2  execute (@sql2) -- Summary by ethnicity
    if @x = 3  execute (@sql3) -- Summary by School
    if @x = 4  execute (@sqltemp1) -- Make temp files for combing
--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------

    set @yr1 = @yr1-1
    set @dbyrstu = 'DST'+cast(@yr1-2000 as varchar(5))+'000CUSD..stu'
    set @dbyrads = 'DST'+cast(@yr1-2000 as varchar(5))+'000CUSD..ads'
    set @dbyrdsp = 'DST'+cast(@yr1-2000 as varchar(5))+'000CUSD..dsp'

end

if @x = 4 and @startyr = 2017 and @Endyr = 2016
begin
    select a.*,b.[Total Suspension - 2016]
    ,cast(cast((cast(a.[Total Suspension - 2017]as real)/cast(b.[Total Suspension - 2016] as real))*100 as decimal(10,0)) as varchar (25))+' %' [Change (2017/2016)*100]
    --,cast(cast(((cast(-a.[Total Suspension - 2017]as real)+cast(b.[Total Suspension - 2016] as real))*100/(cast(a.[Total Suspension - 2017] as real))) as decimal(10,0))  as varchar (25))+'%' [% Change]
    from amc..tempke2017 a left join amc..tempke2016 b
    on a.sc = b.sc
end


-- Remove temp tables - see @x = 4 above
set @yr1 = @startyr
while @yr1 >= @yr2 and @x = 4 and @startyr = 2017 and @Endyr = 2016
begin
     set @sqltemp2 = 'drop table amc..tempke'+cast(@yr1 as varchar(5))
     execute (@sqltemp2) 
     -- print @sqltemp2
     Set @yr1 = @yr1-1
end

0 个答案:

没有答案