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

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;

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

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.

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.

Call one stored procedure to another stored procedure in DB2

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;

Resources