【问题标题】:Converting advanced stored procedure from T-SQL to PL-SQL [closed]将高级存储过程从 T-SQL 转换为 PL-SQL [关闭]
【发布时间】:2013-01-29 10:50:24
【问题描述】:

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

提前致谢。

【问题讨论】:

  • 你可能想花几句话来谈谈存储过程应该做什么以及你的翻译有什么问题。
  • 您好,欢迎来到 Stack Overflow!首先,翻译通常是题外话,但是当你发布了你想要翻译的内容时,谢谢!这使您的问题变得更好,更容易回答。但是,我们还需要知道您的翻译有什么问题。为什么它不起作用?你有没有得到任何错误,它们是什么?它不提供相同的结果吗?您期望的结果是什么?尝试执行的程序是什么?
  • 大多数情况下,您不需要在 Oracle 中使用临时表的所有这些变通方法,因为 Oracle 在您选择行时不会锁定它们。如果您可以只使用一个查询,那么在 Oracle 中会更好

标签: sql tsql stored-procedures plsql oracle11g


【解决方案1】:

您不能直接在过程中创建表。您必须使用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;
/

【讨论】:

  • 嗨 a_house_with_no_name,我使用了您在 pl/sql 中转换的存储过程,但是当我运行该过程时出现如下错误:错误(12,15):PL/SQL:SQL 语句被忽略
  • 您好@a_house_with_no_name,我使用了您在 pl/sql 中转换的存储过程,但是当我运行该过程时出现如下错误:错误(12,15):PL/SQL:SQL 语句被忽略,错误(12,32):PL/SQL:ORA-00942:表或视图不存在,错误(37,1):PL/SQL:SQL 语句被忽略,错误(42,22):PL/SQL:ORA -00942:表或视图不存在,错误(46,1):PL/SQL:语句被忽略。提前致谢。
  • @IvanB 不会按原样运行,我在 proc 中使用了虚拟列和表,更改为您的表并尝试纠正那些小错误。
  • 嗨@Dileep,我很抱歉上面的评论,因为我是oracle pl-sql的新手,我没有正确运行程序,你的程序正在运行。您还可以告诉我如何转换上述程序行: 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 takems = datediff(ms, d, getdate()) SELECT cnt = COUNT(*) FROM #temp
  • 对于您的问题,一切都与光标相关。请阅读有关光标属性的内容,以便您了解如何使用。 open Cursor If cur%notfound then exit..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
  • 1970-01-01
相关资源
最近更新 更多