How to do an update on a query having union in postgrsql? - sql

I have 2 tables name test and test_snapshop. in test table i am saving the current data and in test_snapshot i am saving older data. Now i need to do an update against some id values.theer is a chance that some ids exist in test and some in snapshot table.
I tried like this
update test
set column1=value,
column2=value,
column3=value
where column4 in(1,2,3)
union
update test_snapshot
set column1=value,
column2=value,
column3=value
where column4 in(1,2,3)
Both tables have same no and name of column.
The above query is not as expected. what I am doing wrong in this query. can any one help me in this.
I am new to postgresql.

Although I voted to close as a typo, you can express this in Postgres using a single statement. I might go for:
with ids as (
select *
from (values (1), (2), (3)) v(id)
),
t as (
update test
set column1 = value,
column2 = value,
column3 = value
where column4 in (select i.id from ids i)
)
update test_snapshot
set column1 = value,
column2 = value,
column3 = value
where column4 in (select i.id from ids i);
With this structure, you only need to list the id values once.

Related

Conver PL/SQL query to T-SQL that copyies values of row to another row via a select

I have the following PL-SQL query:
UPDATE TABLE
(
Column1, Column2, Column3, Column4
)
=
(
SELECT
Column1, Column2, Column3, Column4
FROM TABLE
WHERE ID_COLUMN = 'ID OFF ROW TO COPY FROM'
)
WHERE ID_COLUMN = 'ID OFF ROW TO COPY TO'
;
It was written a long time ago in PL/SQL and its purpose seams to be to copy the values of a row to another row in the same table.
The application now also uses SQL Server and the query does not work there.
I have tried the following query:
UPDATE TABLE
SET
Column1 = T2.Column1,
Column2 = T2.Column2,
Column3 = T2.Column3,
Column4 = T2.Column4
FROM TABLE T2
WHERE ID_COLUMN= 'ID OFF ROW TO COPY TO'
AND T2.ID_COLUMN = 'ID OFF ROW TO COPY FROM'
That does not give errors but it does not update correctly either.
I checked by doing a simple update on the row to copy from and then running the lower query.
Does not feel very nice to copy a rows values like this, doesn't work either. But not sure if it can be done with a query that is compatible with both t-SQL and PL/SQL
If you need a code, which is executable both on Oracle and SQL Server:
UPDATE Table2 SET
Column1 = (SELECT T1.Column1 FROM Table1 T1 WHERE T1.Id = Table2.Id)
WHERE Id = 1;
Unfortunately, the UPDATE statement differs in features in these dialects of SQL, especially if you need to update one table based on another.

update 3 columns in 1 uppdate

I'm writing a query to update a table. The table has many fields among which there are 3 fields: column1, column2, column3 each of which can be null.
The data is loaded every day. On day 1 a row can come in which
column1 is null,
column2 is not null
column3 is null.
The next day there may be a row where
column1 is not null,
column2 is null,
column3 is null.
The idea is to insert new rows, and those rows which are identical to previously uploaded ones, but which column1, column2, column3 change are not inserted, but updated.
I tried to write a query to update all three fields at once. But in case described above, I will change not null to null.
Does anyone know a way to update any of the fields in a single update query without writing null there?
for example:
update table2 upt
set column1 = n.column1,
column2 = n.column2,
column3 = n.column3
from (select new.column1, new.column2, new.column3, new.column4, new.column5
from table1 new
where new.column4 in (select old.column4 from table2 old
where old.column5 = new.column5
and (old.column1 is null or old.column2 is null or old.column3 is null)
and (new.column1 is not null or new.column2 is not null and new.column3 is not null) n
where upt.date_fielad = '2022-01-01'
and upt.column4 = n.column4
and upt.column5 = n.column5
and (upt.column1 <> n.column1
or upt.column2 <> n.column2
or upt.column3 <> n.column3)
How I can change this query to not update not null fields null?

Update if count is greater than zero otherwise don't do anything

I have two queries, first is a check if value exists in table1, if yes - I'm updating a value in table2, otherwise I return 0 as a count of found values.
It happens really often and I wish I could do that with one query. How would the query look like then?
Maybe you can try something like this. A RETURNING clause will return rows if at least one row was updated in the with clause. It is then verified by an exists check in the select part.
with upd as
(
update table2 t2 set col = 'value4'
where id = ? and exists ( select 1 from table1 t1 where id = ? ) returning *
)
select exists ( select 1 from upd ) :: int as "updated";
This query when run will return 0 ( integer equivalent of boolean false ) when no rows exist and 1 when at least one row was updated.
DEMO
You can join table1 & table2 and then update the value from table2 with table1.

Oracle: Select and Update at the same time

I have three tables:
Table-1: Column1 | Column2 | Column 3
Table-2: Column4 | Column5 | ColumnUpdate
Table-3: Column7 | Column8
I need to copy some rows from Table-2 to Table-3 based on some conditions with Table-1:
My insert-statement looks like this:
INSERT INTO Table-3 (
Column7,
Column8)
SELECT Table-2.COLUMN4, Table-2.COLUMN5
FROM Table-2 INNER JOIN Table-1
ON Table-2.COLUMN4 = TABLE-1.Column1;
However I want to update column: ColumnUpdate" (Table-2) of the selecting row to "1".
So I select some rows and immediately want to update a process column in that row as '1'.
I don't know how to do that. I saw some examples with "OUTPUT" clause or "UPDATE FOR" but I dont exactly know how to use them in my statement.
MERGE
INTO target_table t1
USING (SELECT col1, col2
FROM source_table
WHERE //conditions here) s1
ON (t1.id = s1.id)
WHEN MATCHED THEN
UPDATE SET column_update = '1'
WHEN NOT MATCHED THEN
INSERT (col1, col2)
VALUES (s1.col1, s1.col2)
WHERE (// condition here);
UPDATE
BEGIN
FOR temp_var IN (
SELECT * from table_a
WHERE table_a.col1 = table_b.col1)
LOOP
// INSERTING INTO TARGET TABLE
INSERT INTO table_b
VALUES(temp_var.col1, temp_var.col2);
// UPDATING SOURCE TABLE
UPDATE table_a
SET status = 'COPIED'
WHERE col1 = temp_var.col1;
END LOOP;
END;

Storing multiple values from SQL query to stored procedure variable

I want to do something like the example below
ColumnAVal = Select ColumnA from TableA where....
ColumnBVal = Select ColumnB from TableA where....
Select * from TableB where ColumnC = (value from ColumnA)
Select * from TableC where ColumnD = (value from ColumnB)
I have to get the values from TableA query which has one hefty where clause. And I have to get about 20 different columns from TableA. So, above example won't be a good idea. I am trying to find a better way to save these values, that doesn't require me to use same where clause 20 times.
Basically idea is to get the 20 or so columns from the TableA and save those as variables for later use. That way I can create just one query to save the column values instead of calling it twenty times.
E.g.
#QuantityAvailable = SELECT TOP 1 WLPS_Qty_Available FROM
TBL_Warehouse_Location_Parts_Spec, TBL_Location_Master,
TBL_Warehouse_Master WHERE WLPS_Parts_Spec_ID = #PartSpecID AND WLPS_Part_Type_ID IN ( 0, #PartTypeID ) AND WLPS_Active_Flag = 'Y' AND ( WLPS_Location_ID = #LocationID )
I have to run the same query again and again 20 times. And I would prefer I don't have to, to save some processing.
declare #ColumnAVal varchar(20);
declare #ColumnBVal varchar(20);
select #ColumnAVal = ColumnA , #ColumnBVal = ColumnB from TableA where....
The values used are from the last row in the returned set so it only makes sense when the select returns a single row or it's suitably ordered (less good).
--I have to assume that the where clause is different in each case
Select * from TableB where ColumnC IN (Select ColumnA from TableA where....)
Select * from TableC where ColumnD IN (Select ColumnB from TableA where....)