May I know on how to call an array in stored procedure? I tried to enclosed it with a bracket to put the column_name that need to be insert in the new table.
CREATE OR REPLACE PROCEDURE data_versioning_nonull(new_table_name VARCHAR(100),column_name VARCHAR(100)[], current_table_name VARCHAR(100))
language plpgsql
as $$
BEGIN
EXECUTE ('CREATE TABLE ' || quote_ident(new_table_name) || ' AS SELECT ' || quote_ident(column_name) || ' FROM ' || quote_ident(current_table_name));
END $$;
CALL data_versioning_nonull('sales_2019_sample', ['orderid', 'product', 'address'], 'sales_2019');
2
Answers
First, change your stored procedure to convert selected columns from array to csv like this.
Then call it as:
execute format()
lets you replace all thequote_ident()
with%I
placeholders in a single text instead of a series of concatenated snippets.%1$I
lets you re-use the first argument.ARRAY['a','b','c']::VARCHAR(100)[]
to explicitly make it an array of your desired type.'{"a","b","c"}'::VARCHAR(100)[]
works too.text
. Interestingly, exceeding specified varchar length would just convert it to unlimited varchar, making it just syntax noise.DBFiddle online demo
current_schema()
calls anyway. You could addnew_table_schema
andcurrent_table_schema
parameters and if most of the time you don’t expect them to be used, you can hide them behind procedure overloads for convenience, usingcurrent_schema()
to keep the implicit behaviour. Demo