How to select into varray in Oracle - sql

I'm trying to save a set of ids in array:
create or replace type vrray_4 as varray(4) of varchar2(10);
create or replace procedure get_acl_owner_exchange (id in varchar2 ) is
path_count int;
acl_type out vrray_4;
begin
select owner_id into acl_type from kmc_acl_perm
where permission_name = 'read.'
or permission_name = 'write.'
or permission_name = 'delete.';
end get_acl_owner_exchange;
I'm getting the error:
LINE/COL ERROR
-------- --------------------------------------------------------------------------------
3/17 PLS-00103: Encountered the symbol "VRRAY_4" when expecting one of the following:
:= . ( # % ; not null range default character
The symbol ":=" was substituted for "VRRAY_4" to continue.

You have the OUT parameter in the wrong place and you need to use BULK COLLECT INTO rather than just INTO:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE KMC_ACL_PERM(
owner_id VARCHAR2(10),
permission_name VARCHAR2(10)
)
/
CREATE OR REPLACE TYPE vrray_4 AS VARRAY(4) OF VARCHAR2(10)
/
create or replace PROCEDURE GET_ACL_OWNER_EXCHANGE (
id IN VARCHAR2,
acl_type OUT VRRAY_4
) IS
BEGIN
SELECT owner_id
bulk collect into acl_type
from KMC_ACL_PERM
where permission_name IN ( 'read.', 'write.', 'delete.' );
END GET_ACL_OWNER_EXCHANGE;
/

Does this work for you:
CREATE OR REPLACE TYPE vrray_4 AS VARRAY(4) OF VARCHAR2(10);
/
CREATE TABLE kmc_acl_perm (
owner_id VARCHAR2(10),
permission_name VARCHAR2(10)
);
CREATE OR REPLACE PROCEDURE get_acl_owner_exchange (id IN VARCHAR2) IS
path_count NUMBER;
acl_type vrray_4;
BEGIN
SELECT owner_id BULK COLLECT INTO acl_type
FROM kmc_acl_perm
WHERE permission_name IN ('read.', 'write.', 'delete.');
END get_acl_owner_exchange;
/

Related

Procedure to Insert into table and its columns by given parameters?

i`ll start this with some code...
CREATE OR REPLACE PROCEDURE LOAD_IMAGE_INTO_TABLE (
imgDir varchar2
, imgName varchar2
, destTable varchar2
, destIndex varchar2
, destBlob varchar2)
AS
fHnd bfile;
b blob;
srcOffset integer := 1;
dstOffset integer := 1;
BEGIN
dbms_lob.CreateTemporary( b, true );
fHnd := BFilename( imgDir, imgName );
dbms_lob.FileOpen( fHnd, DBMS_LOB.FILE_READONLY );
dbms_lob.LoadFromFile( b, fHnd, DBMS_LOB.LOBMAXSIZE, dstOffset, srcOffset );
--insert into (Select * From user_tables Where table_name = destTable) values(imgName,b);
--insert into destTable (destIndex, destBlob) values( imgName, b );
commit;
dbms_lob.FileClose( fHnd );
END LOAD_IMAGE_INTO_TABLE;
As you might see, I am trying to create a procedure which puts an image into a table. Target table name and column names (for imgname and blobfile) will be given as parameters.
Both ways I tried didn't work. (see --insert(...) in code)
Maybe you have any idea how to solve this?
Thanks in advance.

How to use sql in procedure

CREATE TABLE emp_table_001
(
emp_id NUMBER(10) PRIMARY KEY,
emp_name VARCHAR2(50)
);
INSERT INTO emp_table_001 VALUES(1,'addeesh');
INSERT INTO emp_table_001 VALUES(2,'arunkumar');
INSERT INTO emp_table_001 VALUES(3,'ashok');
INSERT INTO emp_table_001 VALUES(4,'bharath');
INSERT INTO emp_table_001 VALUES(5,'durairaj');
select * from emp_table_001
CREATE OR REPLACE PROCEDURE proc_1(employee_id IN NUMBER)
IS
employee_name VARCHAR2(50);
BEGIN
SELECT emp_name INTO employee_name FROM emp_table_001
WHERE emp_id = employee_id;
DBMS_OUTPUT_LINE('NAME:'||emp_name);
END;
I having trouble with the above code. Error message is this
Errors: PROCEDURE PROC_1
Line/Col: 8/1 PL/SQL: Statement ignored
Line/Col: 8/27 PLS-00201: identifier 'EMP_NAME' must be declared
The procedure should look like this:
CREATE OR REPLACE PROCEDURE proc_1 (employee_id IN NUMBER)
IS
employee_name emp_table_001.emp_name%TYPE;
BEGIN
SELECT emp_name
INTO employee_name
FROM emp_table_001
WHERE emp_id = employee_id;
DBMS_OUTPUT.PUT_LINE ('NAME:' || employee_name); --> this was wrong
END;

Error in Initializing oracle collection

I have following self contained code which gives following error. I don't seem to find where do I have to initialize the collection.
create or replace type address_type is object
(
address_line varchar2(100),
city varchar2(100),
state varchar2(100)
)
/
create or replace type emp_rec_type is object
(
emp_name varchar2(100),
addr_rec address_type
)
/
create or replace type emp_arr is table of emp_rec_type
/
create table employees
(
emp_name varchar2(100),
addr_rec address_type
)
/
insert into employees values ( 'dave', address_type ( '30 br','br','nj'));
commit;
-- Create a function to return an array
create or replace function get_emp_rec ( p_name in varchar2 )
return
emp_arr
is
i integer;
l_arr emp_arr := emp_arr();
begin
i := 1;
l_arr.extend;
l_arr(l_arr.last).emp_name := 'a';
l_arr(l_arr.last).addr_rec := address_type ( 'a','b','c');
return l_arr;
end;
/
select emp_name from table ( get_emp_rec( 'dave' ))
* ERROR at line 1: ORA-06530: Reference to uninitialized composite ORA-06512: at "DBADMIN.GET_EMP_REC", line
22
You have to initialize emp_rec_type in the collection.
These two line
l_arr(l_arr.last).emp_name := 'a';
l_arr(l_arr.last).addr_rec := address_type ( 'a','b','c');
replace with this linie
l_arr(l_arr.last) := emp_rec_type('a', address_type ( 'a','b','c'));

Procedure created with compilation errors - but can't find error

I have a procedure but I can't find where the error is:
CREATE OR REPLACE PROCEDURE ADD_GRADE (
STUDEN_ID IN VARCHAR2(6),
SEC_ID IN NUMBER(6),
NEW_GRADE IN CHAR (1)
)
AS
BEGIN
UPDATE ENROLLMENT
SET grade = NEW_GRADE
WHERE s_id = STUDEN_ID AND c_sec_id = SEC_ID;
END;
/
Run this query ..
select * from user_errors where name ='ADD_GRADE'
and youll have your error
All respect goes to #MarkLeiber who spotted the error...
Try this instead:
CREATE OR REPLACE PROCEDURE ADD_GRADE (
STUDEN_ID IN VARCHAR2,
SEC_ID IN NUMBER,
NEW_GRADE IN CHAR
)
AS
BEGIN
UPDATE ENROLLMENT
SET grade = NEW_GRADE WHERE s_id = STUDEN_ID AND c_sec_id = SEC_ID;
END;
/

How to Create Stored procedure in DB2

Can you please help me to create a below Oracle procedure in DB2? Same table name with columns are available in DB2 also but below script is not working
CREATE OR REPLACE PROCEDURE sample_proc (ACCT_NO in CHAR,p_cursor out SYS_REFCURSOR)
is
BEGIN
OPEN p_cursor FOR
select sampl1,sample2,sample3
from
table_test b
where
rec_id='A'
and sample3=ACCT_NO ;
END;
If you want a return better use function here are some example to get collection
CREATE TABLE table_test
(
sample1 VARCHAR2 (1000),
sample2 VARCHAR2 (1000),
sample3 VARCHAR2 (1000)
);
insert into table_test ( sample1,sample2 ,sample3)
values ('daftest1','dsdtest1','sstsest3');
insert into table_test ( sample1,sample2 ,sample3)
values ('FAStest1','fstest1','sstsest3');
insert into table_test ( sample1,sample2 ,sample3)
values ('sdtest1','asdtest1','fstest3');
insert into table_test ( sample1,sample2 ,sample3)
values ('test2','test2','test123');
CREATE OR REPLACE TYPE TEST_REC
AS OBJECT (
sample1 VARCHAR2(1000)
,sample2 VARCHAR2(1000)
,sample3 VARCHAR2(1000)
);
CREATE OR REPLACE TYPE TEST_REPORT_TABLE
AS TABLE OF TEST_REC;
CREATE OR REPLACE FUNCTION testing (p_acct_no IN varchar2)
RETURN test_report_table
IS
v_rec test_rec;
v_test_report_table test_report_table := test_report_table();
BEGIN
FOR i IN (SELECT sample1,sample2,sample3
FROM table_test b
--where rec_id='A'
where sample3=p_acct_no)
LOOP
v_rec:=test_rec(NULL,NULL,NULL);
dbms_output.put_line(i.sample1);
v_rec.sample1:=i.sample1;
v_rec.sample2:=i.sample2;
v_rec.sample3:=i.sample3;
v_test_report_table.EXTEND;
v_test_report_table(v_test_report_table.COUNT) :=v_rec;
END LOOP;
RETURN v_test_report_table;
END;
select * from table(testing(p_acct_no=>'sstsest3'))
Or better use bulk collect if you don't need to add rows dynamically
CREATE OR REPLACE FUNCTION JSTRAUTI.testing (p_acct_no IN varchar2)
RETURN test_report_table
IS
v_test_report_table test_report_table := test_report_table();
BEGIN
SELECT test_rec(sample1,sample2,sample3)
bulk collect into v_test_report_table
FROM table_test b
--where rec_id='A'
where sample3=p_acct_no;
RETURN v_test_report_table;
END;
result the same.