使用declare在Oracle中声明局部变量

时间:2018-02-08 06:37:46

标签: oracle loops plsql

在MS SQL和Sybase中,我们将查询的计数分配给变量,如下所示。

declare @rate_count int
select count(1) "Count"  =   @rate_count
from 
(select DISTINCT RATE_CODE from APPROVED_RATE RATE
where RATE.RATE_CHNG_IND = 'Y')

然而,在甲骨文中,我甚至无法宣布?任何帮助 因为取决于值返回,While循环开始像

while (@rate_count > 1)
Begin
--

End

3 个答案:

答案 0 :(得分:1)

当然你可以声明..

Declare 

rate_count number; 

begin 

select count(1) into rate_count
from 
(select DISTINCT RATE_CODE from APPROVED_RATE RATE
where RATE.RATE_CHNG_IND = 'Y');


dmbs_output.put_line(rate_count);
end;

答案 1 :(得分:0)

您可以在Oracle中使用以下内容:

SQL> set serveroutput on;
SQL> declare 
 rate_count int;
begin 
 select count(1) "Count" into rate_count from (select DISTINCT RATE_CODE from APPROVED_RATE RATE where RATE.RATE_CHNG_IND = 'Y');
 dbms_output.put_line('Your Rate Count is : '||rate_count);
end;

答案 2 :(得分:0)

Oracle不允许我们在块的declare部分执行SQL语句。所以你需要做的是:

declare 
    rate_count pls_integer;
begin
    select count(DISTINCT RATE_CODE) 
    into rate_count 
    from APPROVED_RATE RATE
    where RATE.RATE_CHNG_IND = 'Y';

    while rate_count > 1 loop
        ....

小心那个WHILE。除非你减少rate_count或者有其他EXIT条件,否则代码将永远循环。也许你的意思是:

for idx in 1 .. rate_count loop