I’m new to Oracle PL/SQL. In my database I created a table PERSONS:
person_id int
first_name nvarchar(50)
last_name nvarchar(50)
birth_date datetime
email varchar(80)
The first stored procedure tester_sp takes one parameter @sptotest for searching time in millisecond of the words. The other procedure search_sp takes one parameter @word and shows how the word to be searched (in this stored procedure I’m using a LIKE clause).
First stored procedure 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
Second stored procedure 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 + '%'
Executing the storing procedures:
EXEC tester_sp 'search_sp'
The result of execution is:
------------------ 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.
My question is: how to convert the whole process using stored procedures in Oracle database 11g using PL/SQL? Please help. Thanks in advance.
Creating tables:
Stored procedure:
Executing the stored procedure: