I want to make this procedure on Oracle SQL - oracle

I want to make this procedure on Oracle SQL:
CREATE OR REPLACE PROCEDURE STS_OWNER.PRC_CAMBIO_LDC1 IS
CURSOR LDC1 IS
select CASE_NO
from ldc_cases
where case_no in (select barcode
from sts_tracking
where ldc='0'
and duplicated='0');
FOR UPDATE BARCODE;
BARCODE VARCHAR2(50);
BEGIN
OPEN LDC1;
FETCH LDC1 INTO BARCODE;
WHILE LDC1%FOUND LOOP
update sts_tracking
set ldc= 1
WHERE CURRENT LDC1;
FETCH LDC1 INTO BARCODE;
END LOOP;
CLOSE LDC1;
COMMIT;
END;

There is no need for use loop for updating table. Cursor contains only one value (or rather should because you are fetching it into varchar variable).
What type is case_no in ldc_cases? should be varchar.

Kindly format your code ,you can use either Oracle toad client or SQL developer to create and Format a Stored Proc .Below is the syntax of the same.
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (parameter [,parameter]) ]
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception_section]
END [procedure_name];

Related

To get data from Stored Procedure having without parameter

I have a stored procedure proc1 without parameters. I want to extract data from this stored procedure. How can I get that? Could you help me?
Stored procedure:
create procedure proc1
as
begin
select e_id, e_nm, e_sal
from emp
where e_id like 'e%';
end proc1;
You can do this in Oracle 12.1 or above:
create or replace procedure demo
as
rc sys_refcursor;
begin
open rc for select * from dual;
dbms_sql.return_result(rc);
end demo;
This requires an Oracle 12.1 or later client/driver to handle the implicit result set.
For more details, see Implicit Result Sets in the Oracle 12.1 New Features Guide, Tom Kyte's Blog, Oracle Base etc.
Here's one possible solution:
Declaration:
create procedure proc1 (emp_row IN OUT emp%rowtype)
as
begin
select * --e_id, e_nm, e_sal
into emp_row
from emp
where e_id like 'e%';
end proc1;
Use case:
DECLARE
l_emp_row emp%rowtype;
BEGIN
proc1(l_emp_row);
-- Here you can access every column of table "emp", like so:
-- dbms_output.put_line('e_id: ' || to_char(l_emp_row.e_id));
-- dbms_output.put_line('e_nm: ' || to_char(l_emp_row.e_nm));
-- dbms_output.put_line('e_sal: ' || to_char(l_emp_row.e_sal));
END;
Is there anything special that you need to get out of the Procedure?
Cheers
you create a view this query. (recomended)
Oracle procedure is not return any data. So, you do not see any result. Your result get buffer but not print screen. if You need a procedure, insert all data another table.
create procedure proc1
as
begin
insert into new_table select e_id, e_nm, e_sal from emp where e_id like 'e%';
end proc1;
Another way; You create a function. Because function outputs and inputs. This function;
for example Create an Oracle function that returns a table

Oracle SQL Developer PL/SQL return an array

Devs,
I've searched everywhere I can, but I could not find solution to this simple problem.
Situation:
I need to write a procedure where it takes a column name as the input and return all the distinct values present in that column as output. And then I have to use this procedure in some c# code.
In MS server, it is very easy as it will directly give the set of results unlike in PL/SQL.
Script I could write (which is not giving me the result I need):
CREATE OR REPLACE
PROCEDURE GetCol(PARAM IN STRING, recordset OUT sys_refcursor)
AS
BEGIN
OPEN recordset FOR
SELECT DISTINCT(PARAM)
FROM my_table;
END
;
When I try to check the data in the recordset using this code:
DECLARE
l_cursor SYS_REFCURSOR;
l_sname VARCHAR2(50);
BEGIN
GetCol('col_name',l_cursor);
LOOP
FETCH l_cursor INTO l_sname;
EXIT WHEN l_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(l_sname);
END LOOP;
CLOSE
Can someone help me with this code please.
You can also open a ref_cursor for a string value. Please take a look at this:
CREATE OR REPLACE PROCEDURE GetCol(PARAM IN VARCHAR2, recordset OUT sys_refcursor)
AS
QRY varchar2(100);
BEGIN
QRY := 'SELECT DISTINCT '||PARAM||' FROM my_table';
OPEN recordset FOR QRY;
END;
Then:
DECLARE
l_cursor SYS_REFCURSOR;
l_sname VARCHAR2(50);
BEGIN
GetCol('col_name',l_cursor);
LOOP
FETCH l_cursor INTO l_sname;
EXIT WHEN l_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(l_sname);
END LOOP;
END;
Your problem is caused by ambiguity about what PARAM is in the procedure's SELECT statement:
CREATE OR REPLACE
PROCEDURE GetCol(PARAM IN STRING, recordset OUT sys_refcursor)
AS
BEGIN
OPEN recordset FOR
SELECT DISTINCT(PARAM) -- Ambiguity here
FROM my_table;
END;
Does PARAM refer to the table column or to the first parameter of the procedure? Oracle has assumed the parameter. You can explicitly say which like this:
SELECT DISTINCT(my_table.PARAM)
FROM my_table;
You could if appropriate (it probably isn't here) specify the procedure parameter instead:
SELECT DISTINCT(GetCol.PARAM)
FROM my_table;
Generally this is best avoided by:
always using table aliases in column references select statements, and
having a standard for parameter names that makes them less likely to clash e.g. P_PARAM.

Oracle stored procedure to return a list of ids

I'm trying to convert an sql stored procedure to oracle. It's a very trivial procedure. It just returns a list of ids from a table, which is very easy to do in sql.
SQL
CREATE PROCEDURE [dbo].[tspInstalledLanguages]
AS
BEGIN
SELECT language_id from languages
END
I'm a complete novice when it comes to oracle, and I've learned that it's not as straight forward as this.
I've tried the following:
Oracle
CREATE OR REPLACE PROCEDURE tspInstalledLanguages
AS
BEGIN
SELECT LANGUAGE_ID FROM LANGUAGES;
END;
With no luck.
I get a message that it's expecting select into
It is.
Example:
DECLARE
v_authName author.author_last_name%type;
BEGIN
SELECT author_last_name
INTO v_authName
FROM author
WHERE author_key = 'A103';
dbms_output.put_line('Name: '||v_authName);
END;
/
So using your code:
CREATE OR REPLACE PROCEDURE tspInstalledLanguages
IS
v_language NUMBER;
BEGIN
SELECT LANGUAGE_ID
INTO v_language
FROM LANGUAGES;
dbms_output.put_line(v_language);
END;
/

Oracle trigger keywoard :new not found

my Oracle SqlDeveloper (or Oracle Database?) doesn't know the :NEW keyword.
For instance, if I enter the following sample from Oracles website,
when I execute the "create or replace trigger" paragraph, a window "Enter bind variable" pops up and asks for the bind variable ":new".
Shouldn't this ":new" variable be predefined?
(Oracle SQL Developer 4.0.1.14, Oracle DB 11gR2, Windows)
drop table tab1;
create table tab1 (c1 clob);
insert into tab1 values ('testtext');
create or replace trigger trg1
before update on tab1
for each row
begin
dbms_output.put_line('Old value of CLOB column: '||:OLD.c1);
dbms_output.put_line('Proposed new value of CLOB column: '||:NEW.c1);
-- Previously, we couldn't change the new value for a LOB.
-- Now, we can replace it, or construct a new value using SUBSTR, INSTR...
-- operations for a CLOB, or DBMS_LOB calls for a BLOB.
:NEW.c1 := :NEW.c1 || to_clob('<hr><p>Standard footer paragraph.');
dbms_output.put_line('Final value of CLOB column: '||:NEW.c1);
end;
/
set serveroutput on;
update tab1 set c1 = '<h1>Different Document Fragment</h1><p>Different text.';
select * from tab1;
It turned out I just had to press "Apply" in the Bind dialog, and the trigger was created. Obviously a bug in SqlDeveloper. At least there is a workaround...
I added the 'REFERENCING' statement, and tested the code, seems to work just fine--- BTW, you can just turn DBMS output on in SQL dev w/o having to run the command..
CREATE OR REPLACE TRIGGER trg1
BEFORE UPDATE
ON tab1
REFERENCING NEW AS new OLD AS old
FOR EACH ROW
BEGIN
DBMS_OUTPUT.put_line ('Old value of CLOB column: ' || :old.c1);
DBMS_OUTPUT.put_line ('Proposed new value of CLOB column: ' || :new.c1);
-- Previously, we couldn't change the new value for a LOB.
-- Now, we can replace it, or construct a new value using SUBSTR, INSTR...
-- operations for a CLOB, or DBMS_LOB calls for a BLOB.
:new.c1 := :new.c1 || TO_CLOB ('<hr><p>Standard footer paragraph.');
DBMS_OUTPUT.put_line ('Final value of CLOB column: ' || :new.c1);
END;

Converting Oracle stored procedure to MSSQL

I'm trying to convert some Oracle stored procedures to MSSQL I did some basic SP's but I'm having a trouble converting SP's with cursors.
here's a example :-
create or replace procedure PR_DELETE_FOLDER(indexedFolder NUMBER,indexedPath VARCHAR2,userCode VARCHAR2,folpath VARCHAR2,idpath VARCHAR2,folderid NUMBER) as
cursor c1(pfolid in NUMBER) is
SELECT folder.PARENTFOLDER, folder.FOLDERNAME, folder.FOLDERID ,sys_connect_by_path(folder.FOLDERNAME,'>>') FOLDERPATH , sys_connect_by_path(folder.FOLDERID,'>>') FOLDERIDPATH
FROM T_FOLDER folder
START WITH folder.PARENTFOLDER =pfolid
CONNECT BY PRIOR folder.FOLDERID=folder.PARENTFOLDER;
cursor c2(folid in NUMBER) is SELECT DOCUMENTID,DOCUMENTNAME from T_DOCUMENT where folder = folid;
begin
for r1 in c1(folderid) loop
update T_FOLDER set last_update=sysdate,STATUS=0 where folderid=r1.FOLDERID;
update T_FOLDER set foldercount = foldercount-1 where folderid = r1.PARENTFOLDER;
-- insert into T_FOLDER_AUDIT values(SEQ_FOLDER_AUDIT.nextval,r1.FOLDERID,'Delete a folder',userCode,sysdate,sysdate,userCode,userCode,1);
insert into t_historical_event values(SEQ_HISTORICAL_EVENT.nextval,r1.FOLDERID,null,'com.affno.adms.folder.Folder',null,'Updated',userCode,r1.FOLDERNAME,sysdate,sysdate,sysdate,userCode,userCode,1);
for r2 in c2(r1.FOLDERID) loop
update T_DOCUMENT set LAST_UPDATE = sysdate,ISUPDATEINDEXES=1,MODIFIEDBY=userCode,STATUS=0 where documentid = r2.DOCUMENTID;
update T_FOLDER set doccount=doccount-1 where folderid=r1.FOLDERID;
insert into T_DOCUMENT_AUDIT values(SEQ_DOC_AUDIT.nextval,'delete',r2.DOCUMENTID,'Original',sysdate,sysdate,userCode,userCode,1,userCode);
insert into t_historical_event values(SEQ_HISTORICAL_EVENT.nextval,r2.DOCUMENTID,null,'com.affno.adms.document.Document',null,'Deleted',userCode,r2.DOCUMENTNAME,sysdate,sysdate,sysdate,userCode,userCode,1);
end loop;
end LOOP;
end;
I'm not familiar with MSSQL cursors so I got no clue on this issue.
Thanks.
There are examples in the msdn article:
DECLARE CURSOR (Transact-SQL)
USE AdventureWorks2008R2;
GO
DECLARE vend_cursor CURSOR
FOR SELECT BusinessEntityID, Name, CreditRating FROM Purchasing.Vendor
OPEN vend_cursor
FETCH NEXT FROM vend_cursor;
You could try a made for this purpose conversion application such as http://www.convert-in.com/ora2mss.htm --> Unfortunately, at $49 it ain't cheap.

Resources