SQL Select语句作为变量

时间:2015-03-10 10:02:16

标签: sql sql-server tsql

所以我需要使用从一个长选择返回的数据,在另一个选择中多次使用。

我可以使用子查询,但我不会'我想多次复制代码,因为我可能需要修改它。

所以我试图做一些事情,例如声明临时视图或实现相同目的的东西:

DECLARE @myView as view

SET @myView = (select ob.IdOrden, obs.idarticulo, a.descrip, obs.p1,cc.descrip, SUM(isnull(t.Minutos,0)) Minutos
from Ordenes_Bonos ob
left join (select SUM(DATEDIFF(MINUTE ,FechaIni,FechaFin)) Minutos,IdOrden, IdBono, IdEmpleado 
            from dbo.Pers_Tiempos_Bonos 
            group by IdOrden, IdBono, IdEmpleado) t on t.IdOrden = ob.IdOrden and t.IdBono = ob.IdBono
left join Ordenes_bonos_Salidas obs on obs.idorden = ob.idorden and obs.idbono = ob.IdBono
inner join Colores_cartas cc on cc.idcolor = obs.p1
inner join articulos a on a.idarticulo = obs.idarticulo
where ob.Descrip like 'prod%' and t.minutos > 5 and t.minutos < 1500
GROUP BY ob.IdOrden, obs.idarticulo, a.descrip, obs.p1, cc.descrip)

但这不起作用,似乎不允许这样做。我没有权限制作永久视图,因此不能选择。

我想要这样,以后我可以做我的其他查询:

declare @pp float
set @pp = .4
select @pp as factor, avg(cast(Minutos as float)) as TrimmedMeanP from @TestScores a 
where (select count(*) from @myView aa where aa.Minutos <= a.Minutos) >= (select @pp*count(*) from @myView) 
and (select count(*) from @myView bb where bb.Minutos >= a.Minutos) >= (select @pp*count(*) from @myView)

无需每次都重复第一次。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

使用CTE(公用表表达式)执行此操作它基本上是一个临时表,其范围限定为单个查询。

declare @pp float
set @pp = .4

;with CTE as 
    (select 
        ob.IdOrden, 
        obs.idarticulo, 
        a.descrip, 
        obs.p1,cc.descrip, 
        SUM(isnull(t.Minutos,0)) Minutos
    from 
        Ordenes_Bonos ob
        left join 
                (select SUM(DATEDIFF(MINUTE ,FechaIni,FechaFin)) Minutos,IdOrden, IdBono, IdEmpleado 
                from dbo.Pers_Tiempos_Bonos 
                group by IdOrden, IdBono, IdEmpleado) t 
            on t.IdOrden = ob.IdOrden and t.IdBono = ob.IdBono
        left join Ordenes_bonos_Salidas obs 
            on obs.idorden = ob.idorden 
            and obs.idbono = ob.IdBono
        inner join Colores_cartas cc 
            on cc.idcolor = obs.p1
        inner join articulos a 
            on a.idarticulo = obs.idarticulo
where 
    ob.Descrip like 'prod%'
    and t.minutos > 5 
    and t.minutos < 1500
GROUP BY
    ob.IdOrden, 
    obs.idarticulo, 
    a.descrip, 
    obs.p1, 
    cc.descrip)
select
    @pp as factor, 
    avg(cast(Minutos as float)) as TrimmedMeanP 
from @TestScores a 
where 
    (select count(*) from CTE aa where aa.Minutos <= a.Minutos) >= (select @pp*count(*) from CTE) 
    and (select count(*) from CTE bb where bb.Minutos >= a.Minutos) >= (select @pp*count(*) from CTE)

答案 1 :(得分:0)

首先声明动态@sql并设置您的查询。稍后将变量@result声明为表,并将@sql传递给新创建的表。你可以这样的。

有关sp_executesql

的文档
DECLARE @sql AS NVARCHAR(MAX)

SET @sql = (select ob.IdOrden, obs.idarticulo, a.descrip, obs.p1,cc.descrip, SUM(isnull(t.Minutos,0)) Minutos
from Ordenes_Bonos ob
left join (select SUM(DATEDIFF(MINUTE ,FechaIni,FechaFin)) Minutos,IdOrden, IdBono, IdEmpleado 
            from dbo.Pers_Tiempos_Bonos 
            group by IdOrden, IdBono, IdEmpleado) t on t.IdOrden = ob.IdOrden and t.IdBono = ob.IdBono
left join Ordenes_bonos_Salidas obs on obs.idorden = ob.idorden and obs.idbono = ob.IdBono
inner join Colores_cartas cc on cc.idcolor = obs.p1
inner join articulos a on a.idarticulo = obs.idarticulo
where ob.Descrip like 'prod%' and t.minutos > 5 and t.minutos < 1500
GROUP BY ob.IdOrden, obs.idarticulo, a.descrip, obs.p1, cc.descrip)

DECLARE @results AS TABLE 
        ( 
         --table structure
        )    
INSERT INTO @results EXECUTE sp_executesql @sql