日期:2014-05-16  浏览次数:20896 次

查询SQL使用变量的问题
我需要一次执行多条相似的SQL,并union输出结果
其中有三个查询条件在SQL中是复用的
该怎么使用变量的方式?
DB:Oracle 10g
客户端:PLSQL Developer

SQL例子如下,其中的两处ts和unioncode是一致的,是否可用用变量的方式?

select h.cbilltypecode, h.vnote, h.pk_corp, h.ts, '上游单据关闭' yuanyin
  from nc_ic_general_h h
 where h.cgeneralhid in (select datahid
                           from nc_synclog l
                          where l.synclog like '%关闭%'
                            and l.ts >= '2014-03-26 00:00:00'
                            and l.ts <= '2014-04-15 00:00:00'
                            and l.unitcode = '1002'
                            and l.syncflag = '2')
union all
select h.cbilltypecode, h.vnote, h.pk_corp, h.ts, '上游单据不存在' yuanyin
  from nc_ic_general_h h
 where h.cgeneralhid in (select datahid
                           from nc_synclog l
                          where l.synclog like '%不存在%'
                            and l.ts >= '2014-03-26 00:00:00'
                            and l.ts <= '2014-04-15 00:00:00'
                            and l.unitcode = '1002'
                            and l.syncflag = '2')

------解决方案--------------------
引用:
我需要一次执行多条相似的SQL,并union输出结果
其中有三个查询条件在SQL中是复用的
该怎么使用变量的方式?
DB:Oracle 10g
客户端:PLSQL Developer

SQL例子如下,其中的两处ts和unioncode是一致的,是否可用用变量的方式?

select h.cbilltypecode, h.vnote, h.pk_corp, h.ts, '上游单据关闭' yuanyin
  from nc_ic_general_h h
 where h.cgeneralhid in (select datahid