postgres `insert on conflict update` multiple columns [duplicate] - postgresql

This question already has answers here:
How to update all columns with INSERT ... ON CONFLICT ...?
(2 answers)
Closed 4 years ago.
In this Postgres query,
INSERT INTO TB_PO
SELECT * FROM temporary_table
ON CONFLICT (id) DO UPDATE
SET id = excluded.id;
Since both the tables tb_po and temporary_table are identical with 26+ columns, is there a way I can specify after the SET, that it will set all columns of the affected row? So that I don't have to manually input each column with SET.
thanks

You could avoid some typing by generating your statement based on the results of
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'TB_PO';

Related

How to get the referenced table and column names for a constraint from the information schema in postgresql [duplicate]

This question already has an answer here:
How to list all columns with it's reference in postgresql like in mysql?
(1 answer)
Closed 6 months ago.
In mysql, I can run the following query which would give me the tables and columns used in a foreign relationship:
SELECT CONSTRAINT_NAME as name,
TABLE_NAME as parent_table,
COLUMN_NAME as parent_column,
REFERENCED_TABLE_NAME as referenced_table,
REFERENCED_COLUMN_NAME as referenced_column
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
where table_schema = 'public'
and REFERENCED_TABLE_NAME is not null
How do I get the equivalent information in postgresql? The key_column_usage table in postgres doesn't seem to have the referenced table and column names.
Maybe this helps you :?
SELECT *
FROM information_schema.columns
WHERE column_name like '%NAME%'
AND table_name not like '%NAME%'

Subquery not working with NULL data [duplicate]

This question already has answers here:
How does 'in' clause works in oracle
(5 answers)
NOT IN (subquery) producing zero rows
(2 answers)
SQL "select where not in subquery" returns no results
(12 answers)
Why is nothing selected in this select query?
(5 answers)
Correct way to use "NOT IN" Postgres
(4 answers)
Closed 5 years ago.
Today I have seen one weird issue in PostgreSQL query. In which I have 2 tables Products and Parameters.
Purpose:
It's very simple query, I just want to list all products which are not
there in parameter table.
Query:
select
id
from product_proudct
where active=True and
id not in
(
select distinct product_id from parameter_view
)
Issue:
It won't list anything if there is null value in
parameter table in product_id column. I have verified by just removing null record from the parameter table by adding where clause.
select distinct product_id from parameter_view where product_id is not null
And then it's working fine but with null it won't work, it's really
strange for me.
I would like to know the reason why subquery not working well with null ?

sas macro for appending 24 tables [duplicate]

This question already has answers here:
Merge multiple tables in sas using loop or macro
(2 answers)
Closed 7 years ago.
Hi I need help to append 24 sas tables. I would like to write a macro that appends 24 tables.
How do I create a do loop with the least amount of typing.
Thanks
proc sql;
create table master as
select * from table1
union all
select * from table2
union all
select * from table3;
quit;
I think you did not need a macro. Just type
data master;
set table1-table24;
run;

Add Column If Not Exists in Postgresql [duplicate]

This question already has answers here:
How to add column if not exists on PostgreSQL?
(11 answers)
Closed 8 years ago.
Is postgresql (9.3.2) can do check the existence of a column before add a new column?
I don't want to create a function just for to check the existence.
Just simply like this :
ALTER TABLE IF NOT EXISTS table_name ADD COLUMN column_name data_type;
You'll need to write your own stored procedure in Plpgsql to check if the table has this column. For this you'll need the tables PG_ATTRIBUTE and PG_CLASS where Postgres stores the schema metadata and in particular the information about columns and tables respectively.
The query whose result you need to check in your stored procedure would be a JOIN like:
SELECT A.ATTNAME FROM PG_ATTRIBUTE A, PG_CLASS C
WHERE A.ATTRELID = C.OID AND A.ATTNAME = 'column_name_check_if_exists' AND C.relname= 'table_name' ;
In DDL, you can only:
Add columns
Remove columns
Add constraints
Remove constraints
Change default values
Change column data types
Rename columns
Rename tables
ALTER TABLE: SYNOPSIS AND EXAMPLES -> http://www.postgresql.org/docs/9.3/static/sql-altertable.html
For validations... you need make "PL/SQL"
So, there is no such query. I should using PLPGSQL.

Update rows from another table [duplicate]

This question already has answers here:
How do I UPDATE from a SELECT in SQL Server?
(38 answers)
Closed 8 years ago.
I have a table with 2 columns, Country and Qty. the field country has distinct acronyms of all the countries. Now my job is to replace those acronyms with the actual country name. Now there is another table with acronyms and corresponding country names. I have to take the values from this second table and update the first one where Acronyms match. Please help..
UPDATE q
SET country = a.country
FROM quantity q
JOIN acronym a
ON a.acronym = q.country