-1

我是 Oracle PL/SQL 的新手。在我的数据库中,我创建了一个表PERSONS

person_id int
first_name nvarchar(50)
last_name nvarchar(50)
birth_date datetime
email varchar(80)

第一个存储过程tester_sp采用一个参数@sptotest来搜索单词的毫秒时间。另一个过程search_sp采用一个参数@word并显示如何搜索单词(在这个存储过程中,我使用了一个LIKE子句)。

第一个存储过程tester_sp

CREATE PROCEDURE tester_sp

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 one email address
SELECT TOP 1 @single_email = email
FROM   persons
WHERE  person_id BETWEEN 321106 AND 325000 AND  email LIKE '%.com'
ORDER  BY person_id

-- This is the list of testword(joy and email)
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

  -- Run the procedure and read all from disk.
  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 disk.'

  -- Run it again with data in cache.
  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

第二个存储过程search_sp

CREATE PROCEDURE search_sp @word varchar(50) 
AS
SELECT person_id, first_name, last_name, birth_date, email
FROM   persons WHERE email LIKE '%' + @word + '%'

执行存储过程:

EXEC tester_sp 'search_sp'

执行结果是:

------------------ Testing  [search_sp] ----
6146 ms, 10 rows. Word = "joy". Data in disk
5586 ms, 10 rows. Word = "joy". Data in cache.
6280 ms, 1 rows. Word = "omamo@petinosemdesetletnicah.com". Data in disk
5943 ms, 1 rows. Word = "omamo@petinosemdesetletnicah.com". Data in cache.    

我的问题是:如何使用PL/SQL在Oracle数据库11g中使用存储过程转换整个过程?请帮忙。提前致谢。

4

1 回答 1

1

创建表:

create table PERSONS (
    person_id  integer not null, 
    first_name nvarchar2(50 char),
    last_name  nvarchar2(50 char) not null, 
    birth_date date, 
    email      varchar2(80 char),
    PRIMARY KEY (person_id)
);

CREATE global temporary TABLE temp_persons (
    person_id  integer not null,
    first_name nvarchar2(50 char),
    last_name  nvarchar2(50 char) NOT NULL,
    birth_date date,
    email      varchar2(80 char)  NOT NULL,
    PRIMARY KEY (person_id)
) on commit delete rows;

存储过程:

CREATE PROCEDURE tester_sp as
   start        timestamp;
   diff         interval day to second;
   tookms       integer;
   cnt          integer;
   single_email varchar2(80 char);
   msg          varchar2(1000 char);
begin
    -- Select one email address
    SELECT email
      into   single_email
      FROM   persons
      WHERE  person_id BETWEEN 321106 AND 325000 
        AND  email LIKE '%.com'
        and rownum = 1;

    dbms_output.put_line('------------------ Testing ----');

    for r in (
      with testwords as (  
        -- This is the list of testword(joy and email)
        SELECT 1 as no, 'joy' as word from dual
        UNION ALL
        SELECT 4, single_email from dual)
      SELECT word 
      FROM testwords 
      ORDER BY no) loop

        -- Run the procedure twice
        for pass in 1..2 loop
          start := systimestamp;
          INSERT into temp_persons (person_id, first_name, last_name, birth_date, email)
            SELECT person_id, first_name, last_name, birth_date, email
            FROM   persons 
            WHERE  email LIKE '%'||r.word||'%';
          cnt := sql%rowcount;
          diff := systimestamp - start;
          tookms := round(1000 * ( 
            extract(hour from diff)   * 3600 +
            extract(minute from diff) * 60   +
            extract(second from diff))); 
          msg := to_char(tookms)||' ms, '||to_char(cnt)||' rows. Word = "'||r.word||'". ';
          if pass = 1 then
            msg := msg||'Data on disk.';
          else
            msg := msg||'Data in cache.';
          end if;
          dbms_output.put_line(msg);
          rollback; -- instead of 'TRUNCATE TABLE temp_persons'
        end loop;

    end loop;
end;

执行存储过程:

begin
    tester_sp;
end;
于 2013-01-30T20:00:33.967 回答