Aginity Stored Procedure Error - stored-procedures

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;

Related

SnowScripting: SQL compilation error: syntax error line 6 at position 13 unexpected '<EOF>'

I'm new with Snowflake and creating a procedure in Snowflake using Snow Scripting. What could possibly be wrong?
create or replace procedure test_procedure(p_dob date)
returns varchar
language sql
as
declare
v_age NUMBER;
begin
v_age := datediff(year, p_dob, current_date);
return 'Your age is '||v_age::varchar;
end;
Error msg: SQL compilation error: syntax error line 6 at position 17 unexpected ''.
The code is all right using Snowsgihgt UI, though it should be CURRENT_DATE() instead of CURRENT_DATE. To make it run using ClassicUI is should be delimited with $$:
create or replace procedure test_procedure(p_dob date)
returns varchar
language sql
as
$$
declare
v_age NUMBER;
begin
v_age := datediff(year, p_dob, current_date());
return 'Your age is '||v_age::varchar;
end;
$$;
call test_procedure('2022-01-01');
More at: Using String Constant Delimiters Around a Block in a Stored Procedure

Snowflake stored procedure conditional force error

I want the procedure to fail in case of any conditions been met.
Is there a way to conditionally exit the Snowflake stored procedure with error.
RAISE could be used.
Raises an exception.
-- Stored Procedure - Snowflake Scripting
CREATE OR REPLACE PROCEDURE test_proc(ARG INT)
RETURNS INT
LANGUAGE SQL
AS
DECLARE
my_exception EXCEPTION (-20002, 'Raised MY_EXCEPTION.');
BEGIN
IF (ARG = 2) THEN
RAISE my_exception;
END IF;
RETURN ARG;
END;
Test:
CALL test_proc(1);
-- 1
CALL test_proc(2);
-- -20002 (P0001): Uncaught exception of type 'MY_EXCEPTION' on line 5 at position 8:
-- Raised MY_EXCEPTION.

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

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