sql脚本返回的值应该用作下一个脚本的输入

时间:2015-06-02 14:24:15

标签: mysql sql oracle

1.以下查询返回一个值:

dataGridBasket

2.上述查询返回的值必须如下使用 如果a和b之间的值执行此操作 如果d和c之间的值执行此操作 我知道我必须使用案例陈述。我不知道如何将值作为输入传递给我要编写的新sql脚本

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

按如下方式修改查询的开头:

DECLARE @x int
DECLARE @y int
DECLARE @z float

从左边选择@ x = x,@ y = y,@ z =(x / cast(y as float)* 100) {其余查询未更改}

现在,您可以在同一查询中的任何后续SQL语句中引用@ x,@ y和@z。如果您需要能够从完全不同的查询中访问它们,那么您需要将它们存储在更像表格的永久性地方。

答案 1 :(得分:0)

只需编写PL / SQL块:

begin
for rec in (
select x , y,(x/cast(y as float)*100) as z from 
 (select count(distinct t1.ST_NUM) as x
    from table2 t2, 
           table1 t1,
             table t3
where 
  t2.CD in ($CD1)
  and t2.ITM_CD=($ITM_CD)
  and t2.div=($div)
  and t2.div= t1.div
   and t2.scn_cd = t1._scn_cd
   and t1.week_end =t3.week_end 
   and t3.week_end between ($startdate_1)  and  ($enddate_1))a1
   cross join 
   (select count(distinct t1.ST_NUM) as y
         from from table2 t2, 
                   table1 t1,
                   table t3
where 
  t2.CD in ($CD1)
  and t2.ITM_CD=($ITM_CD)
  and t2.div=($div)
  and t2.div= t1.div
   and t2.scn_cd = t1._scn_cd
   and t1.week_end =t3.week_end 
   and t3.week_end between ($startdate_2)  and  ($enddate_2))a2
) loop
  -- do here your if/case logic. Something like this:
   case 
    when rec.z between a and b then ....
    when rec.z between c and d then ....
  end case;
end loop;
end;
/