在PL / SQL中声明查询的一部分语句

时间:2017-08-08 09:55:05

标签: oracle plsql union declare

我有如下查询:

select t1.varid, t1.var1, t1.var2, t1.vardata, t2.varid, t2.var1, t2,.var2 
from   table1 t1, table2 t2
where  t1.varid = t2.varid
and    t1.vardata between to_date('20170807','YYYYMMDD') and to_date('20170808','YYYYMMDD')

现在的问题是: 我需要不仅在上面的查询中指定的数据范围中提取数据,而且我想在同一查询中提取许多其他范围。

为此,我使用UNION ALL并且它有效。问题是我必须多次重写这个部分:

select t1.varid, t1.var1, t1.var2, t1.vardata, t2.varid, t2.var1, t2, var2 
from   table1 t1, table2 t2
where  t1.varid = t2.varid

是否有关于如何将select t1.varid...定义为命名变量的提示,以避免长时间无聊的重复查询?

欢迎任何其他解决方案。

2 个答案:

答案 0 :(得分:0)

正如聊天中所讨论的那样,您可以按照以下方式进行操作:

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Users\\mushou8\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'install',
1 verbose cli   '-g',
1 verbose cli   'create-react-app' ]
2 info using npm@5.2.0
3 info using node@v6.11.2
4 verbose stack Error: Cannot find module 'json-parse-helpfulerror'
4 verbose stack     at Function.Module._resolveFilename (module.js:469:15)
4 verbose stack     at Function.Module._load (module.js:417:25)
4 verbose stack     at Module.require (module.js:497:17)
4 verbose stack     at require (internal/module.js:20:19)
4 verbose stack     at Object.<anonymous> (C:\Users\mushou8\AppData\Roaming\npm\node_modules\npm\node_modules\read-package-json\read-json.js:12:16)
4 verbose stack     at Module._compile (module.js:570:32)
4 verbose stack     at Object.Module._extensions..js (module.js:579:10)
4 verbose stack     at Module.load (module.js:487:32)
4 verbose stack     at tryModuleLoad (module.js:446:12)
4 verbose stack     at Function.Module._load (module.js:438:3)
5 verbose cwd C:\Users\mushou8
6 verbose Windows_NT 6.1.7601
7 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\mushou8\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "install" "-g" "create-react-app"
8 verbose node v6.11.2
9 verbose npm  v5.2.0
10 error code MODULE_NOT_FOUND
11 error Cannot find module 'json-parse-helpfulerror'
12 verbose exit [ 1, true ]

答案 1 :(得分:0)

您可以尝试直接从这样的表中提取日期,而不是重写重复的语句:

Select 'select t1.varid, t1.var1, t1.var2, t1.vardata, t2.varid, t2.var1, t2,.var2 
From table1 t1, table2 t2 where t1.varid = t2.varid and t1.vardata between to_date('''+MY_DATE+''', ''yyyymmdd'')'  from tableofdate 

以下是我使用的架构。这只是一个示例。请根据您的要求进行编辑

create table tableofdate(my_date varchar(100))
insert into tableofdate values ('20170808')