我是 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中使用存储过程转换整个过程?请帮忙。提前致谢。