Create directory inside stored procedure plsql - stored-procedures

I have this code:
create or replace PROCEDURE insert_dir(id number, filename varchar2, dir varchar2)
IS
sqlQ varchar2;
BEGIN
sqlQ := 'create directory DIR0001 as '||dir;
EXECUTE IMMEDIATE (sqlQ);
END;
I am trying to create a directory inside stored procedure and pass the value by parameter but i get the error:
Package or function INSERT_DIR is in an invalid state
06575. 00000 - "Package or function %s is in an invalid state"
*Cause: A SQL statement references a PL/SQL function that is in an
invalid state. Oracle attempted to compile the function, but
detected errors.
*Action: Check the SQL statement and the PL/SQL function for syntax
errors or incorrectly assigned, or missing, privileges for a
referenced object.
call procedure:
CALL insert_dir(3,'pic.jpg','C:\Users\User\Desktop\media');
Does someone know what am I doing wrong?

After compiling the procedure, it should have said "Procedure created with compilation errors."
If you issue SHOW ERRORS it would have told you the compilation error and which line.
In this case, the error is on this line - the declaration requires a size for the VARCHAR2, e.g.:
sqlQ varchar2(4000);

Related

How to execute a procedure that has OUT parameters

I created this procedure in order to join two tables and select data. The base sql statement works as intended
CREATE OR REPLACE PROCEDURE task1
(p_task1 OUT SYS_REFCURSOR)
AS
BEGIN
OPEN p_task1 FOR
SELECT Department.D#, Department.DNAME, Employee.D#, Employee.Name
FROM Department JOIN Employee ON
Employee.D# = Department.D#;
END task1;
/
However, when I use the command EXECUTE task1 in SQL*Plus, I receive this error:
SQL> EXECUTE task1;
BEGIN task1; END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'TASK1'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
I did some research into the error and its linked to parameters but im unsure what parameters i need, I didn't even intend on coding my procedure to take a parameter.
You have an OUT parameter... but still a parameter, in you procedure you have declared the parameter as
(p_task1 OUT SYS_REFCURSOR)
So when you call task1 you need to provide a location for it to put the output (OUT parameter)
so in your terminal you would have something along the lines of the below....
execute declare ref_cur SYS_REFCURSOR; begin task1(ref_cur); end;
In the above line we have added declare ref_cur SYS_REFCURSOR; which allows you to access the data the procedure fetches.
However a this stage you are just fetching the data and then doing nothing with it. Once retrieved you will need to iterate through the ref_cursor and do whatever it is you need to do with it.

LOAD DATA using Variable filename

I'm trying to write a stored procedure where I pass the path and file name that I want to read. I've tried various ways but always hit a problem, with the error message:
Syntax Error: Unexpected 'pathFile' (Identifier)
Below you can see the simplified version of the procedure I run that's causing the error. Any idea why it might throw syntax error?
CREATE PROCEDURE `test` (IN pathFile VARCHAR(100))
BEGIN
LOAD DATA LOCAL INFILE pathFile INTO TABLE dataNew
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
(dat, x1, x2);
END

Aginity Stored Procedure Error

This may be a layman question. Still I surfed on the web and couldn't get through.
I am getting following error when run simple stored Procedure in Aginity
CREATE OR REPLACE PROCEDURE test()
RETURNS VARCHAR(10)
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
BEGIN
RETURN "SUCCESS"
END
END_PROC;
No error given when running the above.
Get the error ONLY when execute as follows
EXECUTE TEST();
Error:
ERROR [01000] NOTICE: plpgsql: ERROR during compile of TEST near line 3
ERROR [HY000] ERROR: missing ; at end of SQL statement
Thanks
CREATE OR REPLACE PROCEDURE test()
RETURNS VARCHAR(10)
LANGUAGE NZPLSQL AS
BEGIN_PROC
-- No need a DECLARE when you don have to declare anything
BEGIN
RETURN "SUCCESS" ; -- you just need a semi colon!
END; -- also here
END_PROC;

DB2 - How do I create a Stored Procedure that ignores errors?

I am trying to create a simple stored procedure in DB2 that performs the following actions:
Drop table "DELETE_ME"
Create table "DELETE_ME"
Insert into table "DELETE_ME"
Basically, if the table DELETE_ME already exists, the stored procedure executes fine. If it does not exist, then the Stored Procedure fails with the following error:
Lookup Error - DB2 Database Error: ERROR [42704] [IBM][DB2/LINUXX8664] SQL0204N "SARTS02T.DELETE_ME" is an undefined name.
I completely understand this. However, I was hoping to set the procedure up to ignore this error (and all errors) that way the procedure can work as a deployment script as well. On the first deployment, it will ignore the drop table line and jump right to building the table.
I found a CREATE PROCEDURE option called CONTINUE AFTER FAILURE however I am receiving an error. Please see my code and error below. Thanks for your help!!!
CODE:
CREATE OR REPLACE PROCEDURE TEST_PROC LANGUAGE SQL CONTINUE AFTER FAILURE
BEGIN
DROP TABLE DELETE_ME;
CREATE TABLE DELETE_ME (
COLUMN_A DECIMAL(5)
);
INSERT INTO DELETE_ME (COLUMN_A) VALUES (69);
COMMIT;
END;
ERROR:
Lookup Error - DB2 Database Error: ERROR [42601] [IBM][DB2/LINUXX8664] SQL0104N An unexpected token "CONTINUE AFTER FAILURE" was found following "ST_PROC LANGUAGE SQL". Expected tokens may include: "<space>".
The BEGIN ... END section is called a compound statement which allows various declarations and other statements, including other compound statements, to be included inside it.
You'll want to declare a condition handler. You could do it something like this.
CREATE OR REPLACE PROCEDURE TEST_PROC
LANGUAGE SQL
COMMIT ON RETURN YES
BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE '42704' -- or SQLEXCEPTION
BEGIN
-- insert any code here when DROP fails
END;
DROP TABLE DELETE_ME;
CREATE TABLE DELETE_ME
( COLUMN_A DECIMAL(5)
);
INSERT INTO DELETE_ME (COLUMN_A)
VALUES (69);
END;

Simple Oracle Stored Procedure getting Invalid object error

I am trying to write a simple Oracle Stored Procedure:
CREATE OR REPLACE PROCEDURE act.skeleton
IS
DECLARE
v_rowCount NUMBER;
BEGIN
SELECT COUNT(1) INTO v_rowCount FROM ex.emp;
DBMS_OUTPUT.PUT_LINE(v_rowCount);
END;
However, when I try & run the procedure by issuing execute act.skeleton in PL/SQL Developer command window, I get the following error message:
ORA-06550: line 1, column 11:
PLS-00905: object ACT.SKELETON is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
I tried to just run the code without the create procedure statement & it runs successfully with the output being displayed. I have tried both CREATE OR REPLACE PROCEDURE IS & CREATE OR REPLACE PROCEDURE AS options but I still get the same error.
I am not sure if this has anything to do with authorization or visibility of the procedure when I try and execute it or what is causing the act.skeleton object to be invalid. I looked into what the error means & it seems it usually refers to compilation error in the procedure but since I can run the code sans the procedure declaration, I am guessing the declaration piece is the culprit. I just don't know enough to figure out what is causing this and I was hoping that someone will be able to shed some more light on it and point me in the right direction
Thanks,
Ashish
Other Details:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
Don't use the DECLARE keyword in a stored procedure declaration. Remove that and it should compile.

Resources