How to add default values in custom columns in postgres sql - postgresql

I have created Custom columns by
SELECT 'CREATE VIEW myview AS SELECT ' ||
string_agg(
format(
'%I AS %I',
column_name,
column_name|| '_' || '__err_mandatory'
) ,
', '
ORDER BY ordinal_position
) ||
format(' FROM %I.%I', table_schema, table_name) AS C
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'act_1_customer_16510427dfdg66600'
GROUP BY table_name, table_schema;
View is created like below
CREATE VIEW myview AS SELECT __id AS __id___err_mandatory,
col_1_1651042811002 AS
col_1_1651042811002___err_mandatory, col_2_1651042811004 AS
col_2_1651042811004___err_mandatory, col_3_1651042811005 AS
col_3_1651042811005___err_mandatory, col_4_1651042811007 AS FROM
public.act_1_customer_16510427dfdg66600
But i can not add default value to each custom column How to do?

I managed to enter this way.
INSERT INTO myview (cd_ace___err_mandatory, no_desc___err_mandatory) VALUES (200,DEFAULT);
I hope I've helped.

Related

Postgres-11 : Alter Table dynamically

I am trying to alter table based on another table dynamically.
Below is the piece of code i wrote in postgresql stored procedure.
But running out into some syntax errors.
Please help me here.
I just started working in postgresql and i am from sql server background. Like how we do in sql server print stmts do debug dynamic queries inside procedures; do we have any link to refer please share that as well. It would help me.
DROP TABLE IF EXISTS temp_table;
CREATE TEMP TABLE temp_table AS
with cte as
(
select column_name,data_type,character_maximum_length
from information_schema."columns" c
where table_name = 'customer_new' and table_schema = 'public'
and column_default is null
)
,cte1 as
(
select column_name,data_type,character_maximum_length
from information_schema."columns" c
where table_name = 'customer_old' and table_schema = 'public'
and column_default is null
)
select cte.column_name,
case when cte.character_maximum_length is not null then cte.data_type||'('||cte.character_maximum_length||')' else cte.data_type end as data_type
from cte
left join cte1 on cte.column_name = cte1.column_name
where cte1.column_name is null;
for v_column_name,v_data_type in SELECT column_name,data_type FROM temp_table
loop
execute format ('alter table %s add column %s %s ;', v_dump_table_name, v_column_name, v_data_type);
end loop;
DROP TABLE IF EXISTS temp_table;
Thanks in advance.
DROP TABLE IF EXISTS temp_table;
CREATE TEMP TABLE temp_table AS
with cte as
(
select column_name,data_type,character_maximum_length
from information_schema."columns" c
where table_name = 'customer_new' and table_schema = 'public'
and column_default is null
)
,cte1 as
(
select column_name,data_type,character_maximum_length
from information_schema."columns" c
where table_name = 'customer_old' and table_schema = 'public'
and column_default is null
)
select 'alter table '||v_dump_table_name||' add column '||cte2.column_name||' '||data_type
as col
from cte2;
for v_column in SELECT col FROM temp_table
loop
execute format ('%s ;', v_column);
end loop;
DROP TABLE IF EXISTS temp_table;

PostgreSQL how to check table existence?

This is how we can check table existence in MSSQL:
IF OBJECT_ID(N'public."TABLE_NAME"', N'U') IS NOT NULL
select 1 as 'column'
else
select 0 as 'column';
which stores outcome in variable 'column'
How can I do same thing in PostgreSQL ? I want to return 1 or 0 for respective outcome.
Use a SELECT with an EXISTS operator checking e.g. information_schema.tables:
select exists (select *
from information_schema.tables
where table_name = 'table_name'
and table_schema = 'public') as table_exists;
If you can't (or won't) deal with proper boolean values, the simply cast the result to a number (but I have no idea why that should be better):
select exists (select *
from information_schema.tables
where table_name = 'table_name'
and table_schema = 'public')::int as "column";
Note that column is a reserved keyword and thus you need to quote it using double quotes.
Check for column in a table existence use view pg_tables
IF EXISTS ( SELECT attname
FROM pg_attribute
WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'YOURTABLENAME')
AND attname = 'YOURCOLUMNNAME')
THEN
-- do something
END IF;
For my sql use INFORMATION_SCHEMA.COLUMNS
SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
[AND table_schema = 'db_name']
[AND column_name LIKE 'wild']

How to use string aggregation on concatenated result subquery in Postgres?

I am trying to use string aggregation on the results of a subquery that contains concatenated results. I would like to create a long text string containing the qualified name "schema.table" for a DB schema I have in Postgres. Do you know how to achieve it? Below is the code I have come up with (not working), it says
more than one row returned by a subquery used as an expression.
SELECT string_agg(
(SELECT CONCAT(table_schema, '.', table_name) table_schema_name
FROM information_schema.tables
WHERE table_schema = 'name_of_schema'), ' ')
FROM information_schema.tables
WHERE table_schema = 'name_of_schema'
what I have now is:
| table_schema_name |
name_of_schema.table1
name_of_schema.table2
name_of_schema.table3
What I would like to achieve is:
| agrreagated field |
name_of_schema.table1 name_of_schema.table2 name_of_schema.table3
This query should do the task you're asking for:
SELECT string_agg(CONCAT(table_schema, '.', table_name), ' ')
FROM information_schema.tables
WHERE table_schema = 'schema_name'
You should either use the query as table expression in your outer select:
SELECT string_agg(s.table_schema_name, ' ') FROM (
SELECT CONCAT(table_schema, '.', table_name) AS table_schema_name
FROM information_schema.tables
WHERE table_schema = 'public'
) s
or simply avoid the sub-select:
SELECT string_agg(CONCAT(table_schema, '.', table_name), ' ')
FROM information_schema.tables
WHERE table_schema = 'public';

How to add single quotes around a list of columns names?

I have a table which has few columns. I got the name of columns by getting it as an array_agg and then array_to_string. Like below:
"hospitalaccountrecord,locationname,patientkey,inpatientadmitdatetime,readmission,no_null_days_btw_admissions,cohort_assignment,admit_mon_feb,admit_mon_mar,admit_mon_apr,admit_mon_may,admit_mon_june,admit_mon_july,admit_mon_aug,admit_mon_sep,admit_mon_oct,a (...)"
The code I used was this:
select array_to_string(array_agg(column_name::text), ',')
from
(
select column_name
from
information_schema.columns
where table_schema='a' and
table_name = 'b'
order by columns.ordinal_position
) as v;
What I am looking for is the same thing but each column name should be enclosed in ''. Like
'hospitalaccountrecord','locationname','patientkey'
and so on.
Don't need two SELECTs, only
select string_agg(''''|| your_column_name ||'''', ',')
should do the job.
select string_agg(quoted_column_name, ',')
from
(
select '''' || column_name || '''' as quoted_column_name
from
information_schema.columns
where table_schema='a' and
table_name = 'b'
order by columns.ordinal_position
) as v;

SELECT ALL column_names in postgresql

I'm using PostgreSQL and I want to create a query that will display all column_names in a specific table.
Schema: codes
Table Name: watch_list
Here are the column_names in my table:
watch_list_id, watch_name, watch_description
I tried what I found in the web:
SELECT *
FROM information_schema.columns
WHERE table_schema = 'codes'
AND table_name = 'watch_list'
It output is not what I wanted. It should be:
watch_list_id, watch_name, watch_description
How to do this?
If you want all column names in a single row, you need to aggregate those names:
SELECT table_name, string_agg(column_name, ', ' order by ordinal_position) as columns
FROM information_schema.columns
WHERE table_schema = 'codes'
AND table_name = 'watch_list'
GROUP BY table_name;
If you remove the condition on the table name, you get this for all tables in that schema.
SELECT table_name FROM information_schema.tables WHERE table_schema='public'