定义变量

时间:2015-08-21 20:02:19

标签: oracle

我可以将变量输入作为别名是数字吗?

即使我指出NUMBER

,Oracle也会将其转换为CHAR

底部三个很好,因为他们在' '

with main_query as (
  select *
  from table
  where criteria = 1
  order by group, value
),
with min_group as (
  select min(group) from main_query
)
select *
from main_query
where group in (select group from min_group);
  -- this where clause should be fast since there will only be 1 record in min_group

1 个答案:

答案 0 :(得分:0)

Oracle文档说:

DEFINE

DEF [INE] [variable] | [variable = text]

指定用户变量并为其指定CHAR值,或列出单个变量或所有变量的值和变量类型。

所以“define”命令将值保存为chars。

然而,您可以使用to_number将代码中的代码转换为数字,这里是数字变量中成功保存字符(定义值)的示例:

set serveroutput on

DEFINE RecordMonth  = 201401


declare
 Record_Month number;
begin
  select to_number(&RecordMonth) as RecordMonth
  into Record_Month -- number variable
  from dual;
  dbms_output.put_line(Record_Month);
end;

结果是:

201401
PL/SQL procedure successfully completed