在Oracle中选择Variable

时间:2018-02-12 18:21:34

标签: oracle plsql

以下是我将在TSQL中做的事情

declare @Init_Cnt int =1,@Tot_cnt int

set @Tot_cnt = (select count(distinct column_name) from mytable)

while (@init_cnt <= @tot_cnt)
begin
  //Insert statement

end
set @init_cnt = @init_cnt+1;

这是我在Oracle中尝试的内容

declare
    v_Init_cnt INT;
    v_Tot_cnt int;

    set v_Init_cnt := 1;

begin
   select count(distinct column_name) v_Tot_cnt into  from mytable
end

begin
    while v_Init_cnt 
    loop
        dbms_output.put_line ('COUNT': || v_Init_cnt );
        v_Init_cnt 
        exit when v_Init_cnt <= v_Tot_cnt ;
    end loop;
end;

如何在Oracle中实现我的Tsql版本?我做得对吗?如果我想只选择我的变量说

select v_Tot_cnt from dual;无法正常工作我该怎么办?

1 个答案:

答案 0 :(得分:1)

在Oracle中看起来像这样。

DECLARE
        v_Init_Cnt number(10) :=1;
        v_Tot_cnt  number(10);
BEGIN
        select count(distinct column_name) INTO v_Tot_cnt from mytable;

        WHILE (v_Init_Cnt <= v_Tot_cnt)
        LOOP
                --Insert statement
                v_Init_Cnt := v_Init_Cnt+1;
        END LOOP;
END;