简体   繁体   English

SQL Developer,替代变量,基于等式的动态创建

[英]SQL Developer, substitution variable, dynamic creation, based on equation

I want to have a single parameter in my script for a year. 我想在脚本中保留一个参数一年。

define YYYY = 2014;

then I want a 2nd parameter based on this one 然后我要基于这个第二个参数

YYYY2 = &&YYYY - 1;

ie it's 1 year earlier 即是一年前

But from the reading i've done so far this seems to be impossible. 但是从我到目前为止的阅读看来,这似乎是不可能的。

Here's my attempt 这是我的尝试

define YYYY = 2014;
define YYYY2 = &&YYYY - 1;

select &&YYYY, &&YYYY2 from DUAL

select * from cb_enrolment2_&&YYYY;
select * from cb_enrolment2_&&YYYY2 ;

response for last line of code is : 最后一行代码的响应是:

ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause: 
*Action: 
Error at Line: x Column: y

That happens because you assign, literally, 2014 - 1 to YYYY2 . 那是因为你分配,从字面上看, 2014 - 1YYYY2

So: 所以:

select &&YYYY, &&YYYY2 from DUAL

turns into 变成

select 2014, 2014 - 1 from DUAL

which is a valid query, but 这是有效的查询,但是

select * from cb_enrolment2_&&YYYY2

turns into 变成

select * from cb_enrolment2_2014 - 1

which gives you an error. 这给你一个错误。

You can't do math directly on substitution variables, but you can select data from database into them, using column command with new_value argument. 您不能直接对替换变量进行数学运算,但是可以使用带有new_value参数的column命令从数据库中选择数据。 So, nothing stops you from selecting an arithmetical expression from dual : 因此,没有什么可以阻止您从对dual选择算术表达式:

define YYYY = 2014;
column dual_x new_value YYYY2;
select &&YYYY - 1 dual_x from dual;

YYYY2 is created implicitly while executing column and select , so there's no need to define it. YYYY2是在执行columnselect隐式创建的,因此无需define它。

SQLDeveloper uses dialect of SQL*Plus in its worksheet, see this for reference and more details on substitution variables: https://blogs.oracle.com/opal/entry/sqlplus_101_substitution_varia#2_5 SQLDeveloper在其工作表中使用SQL * Plus的方言,请参阅此作为参考以及有关替代变量的更多详细信息: https : //blogs.oracle.com/opal/entry/sqlplus_101_substitution_varia#2_5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM