I want to create a stored procedure that calls multiple other stored procedures and saves the output parameters into variables as each stored procedure's output is the input parameters for the next one.
My code looks like this:
CREATE PROCEDURE sa.sp_step0(inout name text)
LANGUAGE plpgsql
AS $$
DECLARE
id text;
run text;
tables_step2 text[];
tables_step3 text[];
tables_step4 text[];
table_step5 text;
BEGIN
call sa.sp_step1(model_name) insert into id,run;
call sa.sp_step2(id,run) insert into id,run,tables_step2;
call sa.sp_step3(id,run,tables_step2) insert into id,run,tables_step3;
call sa.sp_step4(id,run,tables_step3) insert into id,run,tables_step4;
call sa.sp_step5(id,run,tables_step4) insert into id,run,tables_step5;
return;
end
$$;
I get the following error:
SQL Error [42601]: ERROR: syntax error at or near "insert"ΒΆ
It looks to me as though you're using the syntax of some other database product.
To do what you're trying to do in PostgreSQL, you'd need to declare your procedures something like this:
CREATE PROCEDURE sp_step1(IN model_name TEXT, INOUT id TEXT, INOUT run TEXT)
LANGUAGE plpgsql
AS...
and then call your procedures as
call sa.sp_step1(model_name, id, run);
call sa.sp_step2(id, run, tables_step2);
call sa.sp_step3(id, run, tables_step2, tables_step3);
...
Man I love it when I figure out my own errors and help someone else out there in the www....
basically, if you want to save the outputs of a stored procedure into a variable, you would do the following:
execute ('sa.sp_step1(''' || model_name|| ''')') into id,run;
Related
I keep getting the "SELECT query has no destination for result data" error upon calling this test procedure. What am I doing wrong? I did try adding the RETURN() command prior to SELECT statement but that didn't work either.
CREATE OR REPLACE PROCEDURE SchemaName.SP_Testing_Creating_Procedure (OUT ColumnName VARCHAR(9))
AS $$
BEGIN
SELECT TOP 10 ColumnName FROM SchemaName.TableName where ColumnName is not null;
END;
$$
LANGUAGE plpgsql;
CALL SchemaName.SP_Testing_Creating_Procedure();
As John mentioned you need to put the result into OUT column, examples of using IN, OUT and INOUT parameters you can find here
But if you need to return a few rows as a result, you have to use refcursor
as it's described here
CREATE OR REPLACE PROCEDURE SchemaName.SP_Testing_Creating_Procedure (INOUT result refcursor)
AS $$
BEGIN
OPEN result FOR
SELECT TOP 10 ColumnName
FROM SchemaName.TableName
WHERE ColumnName IS NOT null;
END;
$$
LANGUAGE plpgsql;
Then you can call the stored procedure in a transaction
BEGIN;
CALL logs.SP_Testing_Creating_Procedure('mycursor');
FETCH ALL FROM mycursor;
COMMIT;
another option is temp table which is also described in the above doc
Your procedure is running a SELECT command, but it is not doing anything with the results.
If your intention was to return a result set, you will need to put data in the OUT column.
See: Returning a result set - Amazon Redshift
I have procedure below which create procedure in different projects
CREATE OR REPLACE PROCEDURE `sp_get_data`(arg_ProjectID STRING, arg_DatasetID STRING)
OPTIONS(strict_mode=FALSE)
BEGIN
EXECUTE IMMEDIATE
"CREATE OR REPLACE PROCEDURE `"|| arg_ProjectID || "." || arg_DatasetID || ".sp_get_data_details`() \r\n"||
"BEGIN
-- lengthy code
"END"
;
END;
The code insde procedure is very very lengthy.
When I run this create procedure with OPTIONS(strict_mode=FALSE), it is working..., but it doesn't not save in procedure script..
how to make it persist in procedure script
without it throws error
Query error: Out of stack space due to deeply nested query expression
during query resolutio
But how can I add this dynamically when calling like
call sp_get_data(projectid,datasetid)
Any help,
Highly appreciated
I need to create a stored procedure in IBM netezza SQL database from IBM Aginity workbench.
This is my SQL code to create the SP:
CREATE OR REPLACE PROCEDURE "SP_drop_a_table_if_exists"(varchar(128))
RETURNS boolean
EXECUTE AS OWNER
LANGUAGE NZPLSQL AS
BEGIN_PROC
declare
oname alias for $1;
o record;
begin
select otype into o
from (
select 'TABLE' otype from _v_table where tablename = upper(oname)
union all
select 'VIEW' otype from _v_view where viewname = upper(oname)
) x;
if found then
execute immediate 'DROP '||o.otype||' '||oname;
end if;
end;
END_PROC;
I created successfully.
But, when I ran it,
CALL SP_drop_a_table_if_exists('test_a_table':: varchar(128))
I got error:
ERROR[42S02] error: function 'sp_drop_a_table_if_exists(varchar)' does not exists
unable to identify a function that satisdy the given argument types
you may need to add explicit typecasts
Any help would be appreciated !
You created your stored procedure with mixed case inside of double quotes...
CREATE OR REPLACE PROCEDURE "SP_drop_a_table_if_exists"(varchar(128))
...but when you call the stored procedure you don't use double quotes, so the name is being converted all upper case.
CALL SP_drop_a_table_if_exists('test_a_table':: varchar(128))
Try this instead:
CALL "SP_drop_a_table_if_exists"('test_a_table':: varchar(128))
I should also mention that more recent versions of NPS support this syntax for the DROP TABLE command:
DROP TABLE TABLENAME IF EXISTS;
I am trying create a function for a trigger like this:
CREATE FUNCTION backup_largeobjects_grant()
RETURNS trigger AS $$
BEGIN
GRANT SELECT ON LARGE OBJECT NEW.loid TO backup;
END; $$
LANGUAGE plpgsql;
But this gives me error when it reaches the NEW.
ERROR: syntax error at or near "NEW"
I've been looking but not understand that I'm doing wrong.
Any idea?
It looks like GRANT ... ON LARGE OBJECT statements can't be parameterised; the object ID will need to appear as a literal integer value.
You can achieve this in a stored procedure by building the statement as a string:
EXECUTE 'GRANT SELECT ON LARGE OBJECT ' || NEW.loid::text || ' TO backup';
If I write a simple function doSomething, I can get its result by executing :
select doSomething() from dual;
But, if I wish to call a procedure that has an OUT cursor being passed to it (along with another int parameter), how do I call that procedure inside a query and access the result of the cursor ?
Calling it inside a query is not compulsory.. its just that I want to access the results of that procedure
You can create a procedure like
CREATE OR REPLACE PROCEDURE your_procedure(out_cursor OUT sys_refcursor)
IS
BEGIN
OPEN out_cursor FOR
SELECT employee_name
FROM employees;
END;
/
Once you create your procedure wrap the procedure in a function which returns a cursor like the following
CREATE OR REPLACE FUNCTION your_function
RETURN sys_refcursor
AS
o_param sys_refcursor;
BEGIN
o_param := NULL;
your_procedure(o_param);
RETURN o_param;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
-- raise
WHEN OTHERS
THEN
-- raise
END your_function;
/
To see the results from sql do as
select your_function from dual;
Update 1
To see result in SQL Developer
Step 1
Double click on your results in SQL Developer
[Results][1]
Step 2 Single Click on the button with dots. That will pop up the values
[Grid][2]
You can Do Something Like This
select doSomething(cursor (select int_col from your_table)) colname from dual
Hope this Help