I am have created a stored procedure in oracle. Now I need to convert the same procedure to SQL Server procedure. Since I am not good at SQL procedure, please help me.
Following is the Oracle Procedure:
create or replace
PROCEDURE "FTSREMOVESPECIALCHAR"
AS strquery VARCHAR2(4000 byte);
stmt VARCHAR2(1000);
l_cursor SYS_REFCURSOR;
TYPE result_rec IS RECORD (iid NUMBER(10, 0),
fulltextdetails VARCHAR2(4000 byte),
regex VARCHAR2(4000 byte));
l_result_rec RESULT_REC;
idValue NUMBER(10, 0);
fulltextdetailsValue VARCHAR2(4000 byte);
fulltextWithoutSplChr VARCHAR2(4000 byte);
regexValue VARCHAR2(4000 byte);
minmatchValue NUMBER(10, 0);
strQueryinsert VARCHAR2(4000 byte);
BEGIN
dbms_output.ENABLE(1000000);
FOR c IN (SELECT table_name
FROM user_tables
WHERE table_name LIKE 'FULLTEXTLOOKTABLE_%')
LOOP
dbms_output.Put_line(c.table_name);
strquery := 'select ID, FullTextDetails, Regex from ' || c.table_name;
BEGIN
OPEN l_cursor FOR strquery;
LOOP
FETCH l_cursor INTO l_result_rec;
Exit when l_cursor%NOTFOUND;
fulltextdetailsValue := l_result_rec.fulltextdetails;
regexValue := l_result_rec.regex;
dbms_output.Put_line('Before :' ||fulltextdetailsValue);
fulltextdetailsValue := regexp_replace(fulltextdetailsValue, '[^[:alnum:] ]', NULL);
dbms_output.Put_line('After : '||fulltextdetailsValue);
dbms_output.Put_line('Before regexValue:' ||regexValue);
regexValue := replace(regexValue, '([\~\-])', '([\~\-])?');
regexValue := replace(regexValue, '(\!)', '(\!)?');
regexValue := replace(regexValue, '([\@])', '([\@])?');
regexValue := replace(regexValue, '(\#)', '(\#)?');
regexValue := replace(regexValue, '([\$s\&])', '([\$s\&])?');
regexValue := replace(regexValue, '(\%)', '(\%)?');
regexValue := replace(regexValue, '(\^)', '(\^)?');
regexValue := replace(regexValue, Q'[']',Q'['']');
strQueryinsert := 'update '||c.table_name||' set fulltextdetails='''||fulltextdetailsValue||''' where id='||l_result_rec.iid;
dbms_output.Put_line('strQueryinsert : ' ||strQueryinsert);
EXECUTE IMMEDIATE
strQueryinsert;
strQueryinsert := 'update '||c.table_name||' set regex='''||regexValue||''' where id='||l_result_rec.iid;
EXECUTE IMMEDIATE
strQueryinsert;
END LOOP;
EXECUTE IMMEDIATE
'commit';
close l_cursor;
END;
END LOOP;
END;
This procedure will get the all tables from the DB that starts with "FULLTEXTLOOKTABLE_"
. The table has 4 columns(ID(int), FullTextDetails(nvarchar), Regex(nvarchar), MinMatchCount(int))
. For each table it will take the value of "FullTextDetails"
column and remove all the special characters and also take the
"Regex" and replace
([\~\-]) with ([\~\-])?
(\!) with (\!)?
([\@]) with ([\@])?
(\#) with (\#)?
([\$s\&]) with ([\$s\&])?
(\%) with (\%)?
(\^) with (\^)?
And update the columns "FullTextDetails" and "Regex"
with the new values. Finally the changes are committed.