将T-SQL转换为PL / SQL匿名块

时间:2018-01-22 04:17:37

标签: sql-server select plsql oracle12c

if @buffersize > 0
begin
    select @count = count(*) 
    from [countries] with (nolock)    
    where (convert(varchar, isnull([countries]. [createdtimestamp], '1900-02-02 00:00:00.000'), 21) > '1900-01-01 00:00:00.000'  
           or convert(varchar, isnull([countries].[modifiedtimestamp], '1900-02-02 00:00:00.000'), 21) > '1900-01-01 00:00:00.000')
end
else
begin
    set @buffersize = @count
end

if @count > @buffersize
begin
    select * 
    from 
        (select 
             row_number () over(order by countries.createdtimestamp asc) as my____row_num,
             countries.countryid, countries.countryname,
         from 
             countries with (nolock) 
         where 
             (convert(varchar, isnull(countries.createdtimestamp, '1900-02-02 00:00:00.000'), 21) > '1900-01-01 00:00:00.000'  
              or convert(varchar, isnull(countries.modifiedtimestamp, '1900-02-02 00:00:00.000'), 21) > '1900-01-01 00:00:00.000') 
        ) p
    where 
        my____row_num >= @from and my____row_num <= @to
end
else
begin
    select 
        row_number () over(order by countries.createdtimestamp asc) as my____row_num,
        countries.countryid, countries.countryname,
    from 
        countries with (nolock)    
    where 
        (convert(varchar,isnull(countries.createdtimestamp, '1900-02-02 00:00:00.000'), 21) > '1900-01-01 00:00:00.000'  
         or convert(varchar, isnull([countries].[modifiedtimestamp], '1900-02-02 00:00:00.000'), 21) > '1900-01-01 00:00:00.000') 
end

1 个答案:

答案 0 :(得分:0)

您的出发点,但需要完成验证并了解如何处理数据:

declare
    l_buffersize number := null;
    l_count number := null;
    l_from number := null;
    l_to number := null;
    l_result_sql sys_refcursor;
begin

if l_buffersize > 0 then

    select count(*) 
    into l_count
    from countries    
    where (nvl( countries.createdtimestamp, to_timestamp( '1900-01-01 00:00:00.000', 'yyyy-mm-dd hh24:mi:ss.ff' )) > to_timestamp( '1900-01-01 00:00:00.000', 'yyyy-mm-dd hh24:mi:ss.ff' )
           or nvl( countries.modifiedtimestamp, to_timestamp( '1900-01-01 00:00:00.000', 'yyyy-mm-dd hh24:mi:ss.ff' )) > to_timestamp( '1900-01-01 00:00:00.000', 'yyyy-mm-dd hh24:mi:ss.ff' ));
else
    l_buffersize := l_count;
end if;

if l_count > l_buffersize then

    open l_result_sql for
    select * 
    from 
        (select 
             row_number () over( order by countries.createdtimestamp asc ) as my____row_num,
             countries.countryid, countries.countryname
         from countries 
         where 
             (nvl( countries.createdtimestamp, to_timestamp( '1900-01-01 00:00:00.000', 'yyyy-mm-dd hh24:mi:ss.ff' )) > to_timestamp( '1900-01-01 00:00:00.000', 'yyyy-mm-dd hh24:mi:ss.ff' )
              or nvl( countries.modifiedtimestamp, to_timestamp( '1900-01-01 00:00:00.000', 'yyyy-mm-dd hh24:mi:ss.ff' )) > to_timestamp( '1900-01-01 00:00:00.000', 'yyyy-mm-dd hh24:mi:ss.ff' ) )
        ) p
    where 
        my____row_num between l_from and l_to;
else
    open l_result_sql for
    select 
        row_number () over( order by countries.createdtimestamp asc ) as my____row_num,
        countries.countryid, countries.countryname
    from 
        countries
    where 
        (nvl( countries.createdtimestamp, to_timestamp( '1900-01-01 00:00:00.000', 'yyyy-mm-dd hh24:mi:ss.ff' )) > to_timestamp( '1900-01-01 00:00:00.000', 'yyyy-mm-dd hh24:mi:ss.ff' ) 
         or nvl(countries.modifiedtimestamp, to_timestamp( '1900-01-01 00:00:00.000', 'yyyy-mm-dd hh24:mi:ss.ff' )) > to_timestamp( '1900-01-01 00:00:00.000', 'yyyy-mm-dd hh24:mi:ss.ff' ));
end if;

/*
-- example for fetch results or you can return cursor 'l_result_sql' to out parameter in procedure or function
declare
    l_my____row_num number;
    l_countryid countries.countryid%type;
    l_countryname countries.countryname%type;
begin
    loop
        FETCH l_result_sql
        INTO l_my____row_num, l_countryid, l_countryname;
        exit when l_result_sql%notfound;
        DBMS_OUTPUT.put_line (l_my____row_num);
        DBMS_OUTPUT.put_line (l_countryid);
        DBMS_OUTPUT.put_line (l_countryname);
    end loop;
end;
close l_result_sql;
*/
end;