Postgresql function to create table
15
votes
2
answers
31291
views
I want to create a function in order to create a table with a specific structure pasing part of the name of the table as an argument so the name of the table is t_ . Similar to this:
CREATE OR REPLACE FUNCTION create_table_type1(t_name VARCHAR(30)) RETURNS VOID AS $$
BEGIN
EXECUTE "CREATE TABLE IF NOT EXISTS t_"|| t_name ||"
(
id SERIAL,
customerid INT,
daterecorded DATE,
value DOUBLE PRECISION,
PRIMARY KEY (id)
)"
END
$$ LANGUAGE plpgsql
Then call it like:
SELECT create_table_type1('one');
Is it possible?
Asked by Alan Cor
(357 rep)
May 23, 2013, 04:19 PM
Last activity: Dec 6, 2022, 09:13 AM
Last activity: Dec 6, 2022, 09:13 AM