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.
Related
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
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
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;
Problem that I was facing is that I have to call one stored procedure from another stored procedure in DB2 database. Kind example that I am giving right below.
I have one stored procedure:
CREATE OR REPLACE PROCEDURE Proc1()
IS
Declare myName in varchar;
BEGIN
Select fname into myName from student where fname='x'; // is returning unique value
-- here call anoher proc2
END;
Now so this proc1 procedure is going to call this proc2 procedure.
Now I have second stored procedure:
CREATE OR REPLACE PROCEDURE Proc2(Name in varchar)
IS
BEGIN
-- do anything
END;
I solved this problem,
So solution is like If we want to execute proc using sql command then syntex is like below,
call Proc2('My Name');
We can use this same approach inside our proc also.
For that we have to follow some steps. Lets say that our above sql call is statement that we want to execute. we are going to convert that statement into String and pass necessary parameter by concating variable values. Then execute statement.
CREATE OR REPLACE PROCEDURE Proc1()
IS
Declare myName in varchar;
-- stmt variable is to execute our proc
STMT VARCHAR(4000);
BEGIN
Select fname into myName from student where fname='x'; // is returning unique value
-- this is our logic
STMT :='call Proc2('||myName||')';
EXECUTE IMMEDIATE STMT;
END;
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.