-1

我想手动将存储过程从 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

提前致谢。

4

1 回答 1

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;
/
于 2013-01-29T11:05:20.070 回答