I want to convert manually below stored procedure from T-SQL(MS SQL Server 2008) to P-SQL(Oracle DB 11g). I recently try to convert this procedure using SQL Developer and SwisSQL Tool, but without success. This stored procedure contains one parameter @sptotest for the searching one word. Here is the code:
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
Thanks in advance.
You can’t create a table inside a procedure directly. You have to use
execute immediateto create a global temporary table. You would need to usedeclareA cursor declaration must be in declaration part that mean above the
beginstatement.I tried to correct the possible errors and I didn not test it, because I don’t have an instance. Please do the modifications as needed based on my example.
Small Example for Execute Immediate #