将高级存储过程从T-SQL转换为PL-SQL

时间:2013-01-29 10:50:25

标签: sql tsql stored-procedures plsql oracle11g

我想在存储过程下手动转换从T-SQL(MS SQL Server 2008)到P-SQL(Oracle DB 11g)。我最近尝试使用SQL Developer和SwisSQL Tool转换此过程,但没有成功。该存储过程包含一个参数@sptotest,用于搜索一个单词。这是代码:

T-SQL:

CREATE PROCEDURE sp_name @sptotest sysname AS

DECLARE @d            datetime,
       @tookms       int,
       @cnt          int,
       @single_email varchar(80),
       @word         varchar(50)

DECLARE @testwords TABLE
       (no   int         NOT NULL PRIMARY KEY,
        word varchar(80) NOT NULL)

CREATE TABLE #temp(person_id  int          NOT NULL PRIMARY KEY,
                  first_name nvarchar(50) NULL,
                  last_name  nvarchar(50) NOT NULL,
                  birth_date datetime     NULL,
                  email      varchar(80)  NOT NULL)



SELECT TOP 1 @single_email = email
FROM   persons
WHERE  person_id BETWEEN 321106 AND 325000 AND  email LIKE '%.com'
ORDER  BY person_id


INSERT @testwords(no, word)
  SELECT 1, 'joy'
  UNION ALL
  SELECT 4, @single_email

PRINT '------------------ Testing ' + ' ' + quotename(@sptotest) + ' ----'

DECLARE cur CURSOR STATIC LOCAL FOR
  SELECT word FROM @testwords ORDER BY no
OPEN cur

WHILE 1 = 1
BEGIN
  FETCH cur INTO @word
  IF @@fetch_status <> 0
     BREAK

  TRUNCATE TABLE #temp

  CHECKPOINT
  DBCC DROPCLEANBUFFERS WITH NO_INFOMSGS


  SELECT @d = getdate()
  INSERT #temp
     EXEC @sptotest @word
  SELECT @tookms = datediff(ms, @d, getdate())
  SELECT @cnt = COUNT(*) FROM #temp
  PRINT ltrim(str(@tookms)) + ' ms, ' +
        ltrim(str(@cnt)) + ' rows. Word = "' + @word + '".'


  TRUNCATE TABLE #temp
  SELECT @d = getdate()
  INSERT #temp
     EXEC @sptotest @word
  SELECT @tookms = datediff(ms, @d, getdate())
  SELECT @cnt = COUNT(*) FROM #temp
  PRINT ltrim(str(@tookms)) + ' ms, ' +
        ltrim(str(@cnt)) + ' rows. Word = "' + @word + '". Data in cache.'

END

DEALLOCATE cur

提前致谢。

1 个答案:

答案 0 :(得分:0)

您无法直接在过程中创建表。您必须使用execute immediate来创建全局临时表。您需要使用declare

游标声明必须位于begin语句之上的声明部分。

我试图纠正可能的错误而我没有测试它,因为我没有实例。请根据我的示例根据需要进行修改。

执行立即#

的小例子
begin
  execute immediate 'create table t23 as  select ''1'' aa from dual'; 
  execute immediate 'update t23 set aa =''2'' where aa=''1''';
  COMMIT ;
end;

And your Proc Follows

create or replace procedure test_sp 
(
sptotest in varchar2 default null
)
 as
d date;
tookms number;
cnt number;
single_mail varchar2(80);
word varchar2(50);

cursor cur is select word from testwords order by no;

begin
execute immediate ('create global temporary table testwords
(
no     int          not null,
word   varchar2(80) not null,
PRIMARY KEY(no)
)');

execute immediate ('create global temporary table temp
(
person_id   int           not null,
first_name  nvarchar2(50) null,
last_name   nvarchar2(50) not null,
birth_date  date          null,
email       varchar2(80)  not null,
PRIMARY KEY(person_id)
)');

select Colm_name into Variable_name
from persons
where person_id between 311106 and 32500 and email like '%.com'
order by person_id;

insert into testwords
(
no, 
word
)
select 1, 'joy' from Table_name1
union all
select 4, single_email from Table_name2;

DBMS_OUTPUT.PUT_LINE(  '------------------ Testing '  || ' '  || CAST(sptotest AS  VARCHAR2)  || ' ----');  

open cur; 
 -- Cursor Processing
Close Cur;
end test_sp;
/