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

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

Related

Assigning default value to snowflake stored procedure arguments

Is it possible to have default values in arguments of Stored procedures of Snowflake. For the below example, I am getting error. Please help
syntax error line 1 at position 53 unexpected ''test''.
create or replace procedure test(arg1 string default 'test')
returns string not null
language sql
as
$$
begin
return arg1;
end;
$$
;
Snowflake's procedures applies polymorphism instead of using default value. This solution is when you do not want to call sp like func1(Null)
For example (sql scripting):
create or replace procedure func1(arg1 varchar, arg2 varchar)
...
create or replace procedure func1(arg1 varchar)
...
call func1(arg1 , 'some-default-value')
...
One option could be providing NULL as value and handle it at the begining of the stored procedure with COALSESCE:
create or replace procedure test(arg1 string)
returns string not null
language sql
as
$$
begin
arg1 := COALESCE(arg1, 'test');
return arg1;
end;
$$;
CALL test(NULL);
-- test
Setting a default value/values as arguments directly in Stored procedures is not available in Snowflake currently
The below link can be referred for the allowed syntax in Stored Procedures
https://docs.snowflake.com/en/sql-reference/sql/create-procedure.html#syntax

AWS Redshift overloading procedure name doesn't work

I am investigating how procedure name overloading works in AWS Redshift and keep encountering an issue. Below is a test script:
create or replace procedure sp_test_1(in_int integer)
language plpgsql
as $$
begin
raise notice '1 %', in_int;
end
$$
;
create or replace procedure sp_test_1(in_int integer, in_text varchar)
language plpgsql
as $$
begin
raise notice '2 % %', in_int, in_text;
call sp_test_1(in_int);
end
$$
;
call sp_test_1(1)
;
call sp_test_1(12, '2'::varchar)
;
Error messages:
[Amazon](500310) Invalid operation: sp_test_1(integer) is a procedure
Hint: To call a procedure, use CALL.;
[Amazon](500310) Invalid operation: sp_test_1(integer, character varying) is a procedure
Hint: To call a procedure, use CALL.;
How shoud I modify procedure signatures to make procedure name overloading work?
UPD: version() returns the following
PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.24066
In amazon-redshift, there have no option to check if procedure exist before DROP procedure Check Here.
And in your code, you are creating or replace your procedure . Thus you don't need to drop your procedure before write CREATE OR REPLACE procedure.
Please remove below 2 line from your code.
drop procedure sp_test_1(integer);
drop procedure sp_test_1(integer, varchar);
Then your procedure will run without error.

[Amazon](500310) Invalid operation: unterminated dollar-quoted string at or near "$$

Following the AWS documentation to create a stored procedure via the Redshift Query Editor or SQL Workbench/J gives this error: Amazon Invalid operation: unterminated dollar-quoted string at or near "$$. Here is a generic sample of code I was attempting:
CREATE OR REPLACE PROCEDURE
load_inventory () AS $$
BEGIN
INSERT INTO inventory(sku,qty,location)
SELECT sku, qty, 'location name' FROM location_inventory;
END;
$$
LANGUAGE plpgsql;
How should the statement be written to successfully create a stored procedure in Redshift?
Using single-quotes instead of a dollar-quoted string solved the issue. Note that the string used within the SELECT statement has repeated single quotes.
CREATE OR REPLACE PROCEDURE
load_inventory () AS '
BEGIN
INSERT INTO inventory(sku,qty,location)
SELECT sku, qty, ''location name'' FROM location_inventory;
END;
'
LANGUAGE plpgsql;

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;

PL SQL Procedure warning compiled Error PLS-00103

I'm starting with PL/SQL, this is my first Procedure and I find it difficult to compile; I have tried so many different versions, I carry the last attempt. I don't understand why SQLDEVELOPER say me: "procedure compiled (with errors)".
The compiler say me:" Errore(10,1): PLS-00103: Trovato il simbolo (find) "DECLARE" instead: begin function pragma procedure subtype type current cursor delete exists prior "
If there are other errors (also logical) please tell me. I would like to improve.
Thank you all for the answers
My Procedure:
create or replace PROCEDURE calcola_giorn (giornata IN INTEGER) is
-- si tenga presente che in realtà giornata=idPartita
somma NUMBER;
idcal NUMBER;
nometorn VARCHAR2;
idformaz NUMBER;
nomesquadr VARCHAR2;
DECLARE;
SELECT idcalendario INTO idcal FROM partita WHERE id= giornata;
SELECT nometorneo INTO nometorn FROM calendario WHERE id= idcal;
CURSOR formazioni_di_giornata IS
SELECT id, nomesquadra FROM formazione where idpartita= giornata;
CURSOR giocatori_di_giornata IS
SELECT votogiocatore FROM schiera WHERE idformazione= idformaz;
Begin
OPEN formazioni_di_giornata;
FOR tupla_formazione IN formazioni_di_giornata LOOP
somma:=0;
FETCH formazioni_di_giornata INTO idformaz, nomesquadr;
OPEN giocatori_di_giornata;
FOR tupla_giocatore IN giocatori_di_giornata LOOP
somma:= somma + tupla_giocatore.votogiocatore;
END LOOP;
CLOSE giocatori_di_giornata;
UPDATE partecipa SET punti= somma WHERE ( (nomesquadra= nomesquadr) AND (nometorneo= nometorn));
END LOOP;
CLOSE formazioni_di_giornata;
EXCEPTION WHEN OTHERS THEN raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END calcola_giorn;
Varchar limit should be defined, like varchar2(100)
Replace DECLARE; with BEGIN
All your cursors should go before BEGIN
Put END; for the procedure
You can try compiling after making these changes.

Resources