将SQL Server脚本转换为Oracle

时间:2018-06-12 14:54:27

标签: sql-server oracle translate

我创建了一个在SQL Server中运行良好的脚本,但现在我需要让它在Oracle中运行,并且我在转换脚本时遇到问题。

这是我的SQL Server脚本:

-- run the commented out statement in another query analyzer window to stop the script
-- update ##stopsign set val = 1
set nocount on

--declare variables
declare @morework int
declare @archivecount int
declare @stopsign int

--if working tables exists clear them, if not create and initialize them
if (object_id('tempdb..##stopsign') is null)
    create table ##stopsign (val int)
else 
    delete from ##stopsign

insert into ##stopsign values (0)

if (object_id('tempdb..#tempdins') is null)
    create table #tempdins (tempdin varchar(255) not null, processed int null)
else 
    delete from #tempdins


--initialize #tempdins working table with all the records eligible to be unarchived
--edit the select statement if needed to change the records to be unarchived
insert into #tempdins(tempdin)
select tempdin
from document
where archivestatus = 'C' 
and status = 'U' 
option (MAXDOP 1)


--inialize variables with current values
select @archivecount = (select count(*) from unarchs)
select @stopsign = (select val from ##stopsign)
select @morework = 1


--while there is more to do, unarchs table has less then 1000 records, and the stopsign value is 0 loop
while (@morework >= 1 and @stopsign = 0)
begin
    if @archivecount <1000 
    begin
        -- number to be processed at once
        -- change this value if you would like to dump more in to the unarchs table at once
        set rowcount 100    

        update #tempdins
        set processed = 0
        where processed is null


        --reset rowcount
        set rowcount 0

        --populate the unarchs table with valid values
        --this will unarchive at the page (lowest) level
        insert into unarchs (drawer,foldernumber,packageid,docid,pagenumber,unarchtype,unarchdate,unarchtime,userid,unarchdays) 
        select distinct drawer,foldernumber,packageid,docid,pagenumber,'Page','20061128','12:00:00','ADMIN',360
        from document 
        where tempdin in (select tempdin
                from #tempdins
                where processed = 0)

        --update with rowcount to see if finished
        select @morework = @@rowcount

        --set the tempdins to processed in working table
        update #tempdins
        set processed = 1
        where processed = 0

        --get new counts for variables for evaulation
        select @archivecount = (select count(*) from unarchs)
        select @stopsign = (select val from ##stopsign)

        --wait a second so the CPU doesn't spin
        waitfor delay '00:00:01'
    end
    else
    begin
        --get new counts for variables for evaulation
        select @archivecount = (select count(*) from unarchs)
        select @stopsign = (select val from ##stopsign)

        --wait a second so the CPU doesn't spin
        waitfor delay '00:00:01'
    end
end


set nocount off

到目前为止,我对ORACLE的所有内容(用PL / SQL编写):

-- run the commented out statement in another query analyzer window to stop the script
-- update ##stopsign set val = 1

--if working tables exists clear them, if not create and initialize them
declare
v_sql LONG;
begin

v_sql:='CREATE GLOBAL TEMPORARY TABLE STOPSIGN;
(
VAL int
)';
execute immediate v_sql;

EXCEPTION
    WHEN OTHERS THEN
      IF SQLCODE = -955 THEN
        NULL; -- suppresses ORA-00955 exception
      ELSE
        delete from STOPSIGN;

      END IF;
END; 
/

insert into STOPSIGN values (0);


--if working tables exists clear them, if not create and initialize them
declare
v_sql LONG;
begin

v_sql:='CREATE GLOBAL TEMPORARY TABLE TEMPDINS;
(
tempdin varch(255),
processed int null
)';
execute immediate v_sql;

EXCEPTION
    WHEN OTHERS THEN
      IF SQLCODE = -955 THEN
        NULL; -- suppresses ORA-00955 exception
      ELSE
        delete from TEMPDINS;

      END IF;
END; 
/



--initialize #tempdins working table with all the records eligible to be unarchived
--edit the select statement if needed to change the records to be unarchived
insert into TEMPDINS(tempdin)
select * from (select tempdin
from document join packtype on packtype.packagetype=document.packagetype
and archivestatus = 'C' and ADRIVE is not null
and ADRIVE <> DRIVE ) where ROWNUM < 10;

--inialize variables with current values
Declare
archivecount int;
stopsign int;
morework int;
Begin
 Select count(*) INTO archivecount from UNARCHS;
 Select VAL into stopsign from STOPSIGN;
 morework := 1;
END

--while there is more to do, unarchs table has less then 1000 records, and the stopsign value is 0 loop
WHILE morework > 0 and stopsign = 0
LOOP{
begin
  if archivecount <1000 
  begin
    -- number to be processed at once
    -- change this value if you would like to dump more in to the unarchs table at once
    set rowcount 100  

    update TEMPDINS
    set processed = 0
    where processed is null

  }
    --reset rowcount
    set rowcount 0

    --populate the unarchs table with valid values
    --this will unarchive at the page (lowest) level
    insert into UNARCHS (drawer,foldernumber,packageid,docid,pagenumber,unarchtype,unarchdate,unarchtime,userid,unarchdays) 
    select distinct drawer,foldernumber,packageid,docid,pagenumber,'Page','20061128','12:00:00','ADMIN',360
    from DOCUMENT 
    where tempdin in (select tempdin
          from TEMPDINS
        where processed = 0)

    --update with rowcount to see if finished
    select morework = select NUM_ROWS into morework from user_tables where table_name = 'UNARCHS'

    --set the tempdins to processed in working table
    update TEMPDINS
    set processed = 1
    where processed = 0

    --get new counts for variables for evaulation
    select archivecount = (select count(*) from unarchs)
    select stopsign = (select val from STOPSIGN)

    --wait a second so the CPU doesn't spin
    waitfor delay '00:00:01'
  end
  else
  begin
    --get new counts for variables for evaulation
    select archivecount = (select count(*) from unarchs)
    select stopsign = (select val from STOPSIGN)

    --wait a second so the CPU doesn't spin
    waitfor delay '00:00:01'
  end
end
End
END IF
END LOOP

任何帮助将不胜感激。我的公司没有Oracle资源可供我使用,Google也厌倦了我。

1 个答案:

答案 0 :(得分:1)

我认为你可以摆脱一切,只做一个插入声明:

insert into unarchs (drawer,foldernumber,packageid,docid,pagenumber,unarchtype,unarchdate,unarchtime,userid,unarchdays) 
select distinct drawer,foldernumber,packageid,docid,pagenumber,'Page','20061128','12:00:00','ADMIN',360
from DOCUMENT
where archivestatus = 'C'
And status = 'U';

我假设你有一个不同的进程来更新文档表中的行,所以这个过程不会经常拾取相同的行?