简体   繁体   English

声明变量并在查询 oracle 中使用它们

[英]Declare variables and use them in query oracle

I'm in the middle of data migration, converting Sybase query in oracle 11g I've stuck on this thing from past 2 days我正在进行数据迁移,在 oracle 11g 中转换 Sybase 查询我从过去 2 天开始就一直坚持这件事

@Declare @myDate Datetime
Select @myDate = workingDate from MyTable

Then there are few sql statement in which the varaible myDate is getting used in where clause然后很少有 sql 语句在 where 子句中使用了变量 myDate

For Ex对于前

Select * form table1 
join table1 on table1.id = table.id
join table1 on table1.id =  mytable.id
where mytable.workingDate = my_date

// so at last what I want is to declare one variable which will gets its value from a select clause and use it in further sql statements in a complex query // 所以最后我想要声明一个变量,该变量将从 select 子句中获取其值,并在复杂查询中的进一步 sql 语句中使用它

I want to achieve the same in Oracle 11g without any added complexity我想在 Oracle 11g 中实现相同的目标,而不会增加任何复杂性

I'm new to oracle, your help is required.我是 oracle 的新手,需要您的帮助。

Are you using SQLPlus: I don't think you can declare a SQLPlus TIMESTAMP variable.您在使用 SQLPlus:我认为您不能声明 SQLPlus TIMESTAMP 变量。

Try instead a VARCHAR2 variable and convert to TIMESTAMP:尝试使用 VARCHAR2 变量并转换为 TIMESTAMP:

SQL> select banner from v$version where rownum=1;

BANNER
--------------------------------------------------------------------------------
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

SQL> variable ko timestamp;
Usage: VAR[IABLE] [ <variable> [ NUMBER | CHAR | CHAR (n [CHAR|BYTE]) |
            VARCHAR2 (n [CHAR|BYTE]) | NCHAR | NCHAR (n) |
            NVARCHAR2 (n) | CLOB | NCLOB | BLOB | BFILE
            REFCURSOR | BINARY_FLOAT | BINARY_DOUBLE ] ]
SQL> --
SQL> variable my_date varchar2(50) ;
SQL> begin
  2  select workingDate  into :my_date from MyTable;
  3  end;
  4  /

PL/SQL procedure successfully completed.

SQL> print my_date;

MY_DATE
------------------------------
20-JUN-20 10.55.55.264286 AM

SQL> print :my_date;

MY_DATE
------------------------------
20-JUN-20 10.55.55.264286 AM

SQL> --
SQL> select * from mytable;

WORKINGDATE
------------------------------
20-JUN-20 10.55.55.264286 AM

SQL> select workingDate  from MyTable where workingDate = to_timestamp(:my_date, 'DD-MON-YY HH.MI.SSXFF AM');

WORKINGDATE
------------------------------
20-JUN-20 10.55.55.264286 AM

SQL> 

variable doesn't support dates or timestamps. variable不支持日期或时间戳。 Also your final query should use :my_date (indicating a host variable), not my_date .此外,您的最终查询应使用:my_date (表示主机变量),而不是my_date

If the aim is to populate tables using variables populated in a previous step, you could use PL/SQL for the whole task, eg如果目标是使用在上一步中填充的变量来填充表,则可以将 PL/SQL 用于整个任务,例如

declare
    myDate date;
begin
    select some_col into myDate from wherever;

    insert into target_table (col1, col2, col2)
    select x, y, z
    from   source_table
    where  business_date = myDate;
end;

Or better still, define PL/SQL packages to encapsulate your processing logic.或者更好的是,定义PL/SQL 包来封装您的处理逻辑。

Regarding the code you posted, from Oracle 12.1 onwards, you can declare a ref cursor within PL/SQL and have clients such as SQL*Plus simply call the block and print the results:关于您发布的代码,从 Oracle 12.1 起,您可以在 PL/SQL 中声明一个 ref cursor 并让 SQL*Plus 等客户端简单地调用该块并打印结果:

declare
    my_date timestamp;
    results sys_refcursor;
begin
    select systimestamp into my_date
    from   dual;

    open results for
        select my_date as my_date from dual;
    
    dbms_sql.return_result(results);
end;
/
PL/SQL procedure successfully completed.

ResultSet #1

MY_DATE
---------------------------------------------------------------------------
20-JUN-20 10.02.29.130000000

1 row selected.

For earlier Oracle versions you still have to declare the ref cursor as a host variable in SQL*Plus:对于早期的 Oracle 版本,您仍然必须在 SQL*Plus 中将 ref cursor 声明为主机变量:

set autoprint on

var results refcursor

declare
    my_date timestamp;
begin
    select systimestamp into my_date
    from   dual;

    open :results for
        select my_date as my_date from dual;
end;
/

(Or set autoprint off and then print results explicitly.) (或set autoprint off ,然后显式print results 。)

I don't know Java though, so I don't know whether any of the approaches above will work in your environment.虽然我不知道 Java,所以我不知道上述任何方法是否适用于您的环境。

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

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