LOAD DATA using Variable filename - stored-procedures

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

Related

Use of variable SET statement in stored procedures compiles but fires exception at runtime

In a Snowflake SQL-stored procedure, I would like to SET value of a variable like the following:
DROP PROCEDURE IF EXISTS MY_DB.MY_SCHEMA.MY_SP_SQL();
CREATE PROCEDURE MY_DB.MY_SCHEMA.MY_SP_SQL()
RETURNS STRING NOT NULL
LANGUAGE SQL
AS
DECLARE
retValue VARCHAR;
BEGIN
SET retVal = '';
[...]
Everything rocks at compile time but fails at runtime with the following exception
Uncaught exception of type 'STATEMENT_ERROR' on line x at position y :
Stored procedure execution error: Unsupported statement type 'SET'.
despite documentation says this to be possible ๐Ÿคจ
Can someone gently explain to the noob of me? ๐Ÿ˜
Thanks,
Within a SQL stored procedure use LET instead of SET. The syntax for stored procedures (also known as Snowflake Scripting) is different from interactive SQL. The valid commands in SQL scripting are described in the Snowflake Scripting Reference

cant create stored procedure using type varchar 2-pls-00103

i have tried to create a stored procedure using sql developer and oracle database
CREATE OR REPLACE PROCEDURE INSERT_CATALOG
(COUNTRY IN NOCOPY CATALOG.COUNTRY%TYPE)
AS
BEGIN
INSERT INTO CATALOG (COUN_ID, COUNTRY)
VALUES (COUN_ID_SEQ.nextval , COUNTRY);
COMMIT;
END INSERT_CATALOG;
and i was recevingthis kind error on compiling
Error
Error(3,19): PLS-00103: Encountered the symbol "CATALOG" when expecting one of the following: := . ) , # % default character The symbol ":=" was substituted for "CATALOG" to continue.
Please help with this code
Remove NOCOPY.
It can be used with OUT or IN OUT parameters, but not with IN.

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;

Create directory inside stored procedure plsql

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);

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;

Resources