Oracle: Select and Update at the same time - sql

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;

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?

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

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.

Does Oracle MERGE Handles Table Join Automatically?

I am new to ORACLE MERGE Command.
I intend to insert a row into a target table based on a source table data.
The source table has Foreign Key Relation to Target Table. Column1 and Column2 in source table must exist to allow a row with those values in Target Table.
My Merge query looks like this.
MERGE into TARGET_TABLE target
USING (
Select column1, column2 from SOURCE_TABLE where column3='somevalue'
) source
ON (
source.column1 is null or target.column4='anotherValue'
)
WHEN NOT MATCHED THEN INSERT (
target.column1,
target.column2,
target.column3,
target.column4
) VALUES (
source.column1,
source.column2,
'somevalue3',
'anotherValue'
)
It inserts one row and only when it finds that source.column1 is existing for the source.column3 value of 'somevalue' ie not null and,
there is no row in target table whose column1, column2 and column 4 have following values respectively
Same value of column1 in source table when column3 is 'someValue'
Same value of column2 in source table when column3 is 'someValue'
'anotherValue'
I need an explanation as to why it matches Point1 and Point2 above even though my MATCH conditions in 'ON' clause does not include them.
When I coded this I expected multiple rows to NOT MATCH the condition since there are many rows in target table where column4 does not has value anotherValue.
ATTEMPT TO ELABORATE # 1
TABLE 1 (SOURCE)
COL1 COL2 COL3
a1 a2 a3
TABLE 2 (TARGET)
COL1 COL2 COL3 COL4
No row
DESIRED Behavior:
Insert a row in TABLE 2
if source table has a row where COL3 = 'a3' and
if there is no row in target with col1 = a1 and col2 = a2 and col4 = b4
Data to be inserted in Target table is: col1 = a1, col2 = a2 and col4 = b4
OBSERVED BEHAVIOR
Matches the desired behavior above which surprised me. I wondered that I need to have an ON CLAUSE Like this
ON (
source.column1 is null or source.column1 != target.column1 or source.column2 != target.column2 or target.column4 = 'anotherValue'
)
Based on DEMorgan's law:
NOT (A and B) = NOT A or NOT B;
NOT (A OR B ) = NOT A and NOT B

Update target table only if its not null

I have a scenario, where we want to update 40 columns in oracle table. source is another oracle table.
They want to igonore the value of the column if it have null !!
example:
col1 col2 col3
1 null b
2 null 3
target table :
col1 col2 col3
1 a null
2 b null
after updted.
col1 col2 col3
1 a b
2 b 3
note : we have to upate only if its not null ..
any advise is appreciated.
update target_table tt
set (col1, col2, col3) = ( select nvl(st.col1,tt.col1),
nvl(st.col2,tt.col2),
nvl(st.col3,tt.col3)
from source_table st where st.primary_key = tt.primary_key )
where exists ( select null
from source_table st
where st.primary_key = tt.primary_key
and (st.col1 is not null
or st.col2 is not null
or st.col3 is not null) );
Obviously you have to determine what the primary key is. I used "primary_key" only as an instructive guide.
Try
MERGE INTO TARGET_TABLE t
USING SOURCE_TABLE s
ON (s.COL1 = t.COL1)
WHEN MATCHED THEN
UPDATE SET t.COL2 = NVL(t.COL2, s.COL2)
t.COL3 = NVL(t.COL3, s.COL3);
I'm assuming here the COL1 is the column to use to find matching rows in TARGET_TABLE and SOURCE_TABLE.
Share and enjoy.