ORA-01407: cannot update ("schema"."UTILISATEUR"."ID_PERSONNE") to NULL - sql

Can you pleas help me ?
I have this issue ORA-01407: cannot update ("schema"."UTILISATEUR"."ID_PERSONNE") to NULL.
*** But I checked and "schema"."UTILISATEUR"."ID_PERSONNE" is not null !!
Every record is filled.
Below the query :
UPDATE schema.utilisateur
SET schema.utilisateur.ID_Personne = (select testtable.New_ID_Personne
from testtable
where TESTTABLE.Login = schema.utilisateur.Login
and testtable.New_id_personne is not null)

It's probably due to there being logins in the schema.utilisateur table that aren't present in testtable, meaning the corresponding new_id_personne will be null. You need to add a where clause to your update statement to make sure that you're only updating rows that exist in testtable, e.g.:
UPDATE schema.utilisateur su
SET su.ID_Personne = (select testtable.New_ID_Personne
from testtable tt1
where tt1.Login = su.Login
and tt1.New_id_personne is not null)
where exists (select null
from testtable tt2
where tt2.Login = su.Login
and tt2.New_id_personne is not null);
or you could convert it into a merge statement, e.g.:
merge into schema.utilisateur tgt
using (select testtable.New_ID_Personne
from testtable tt
where tt1.New_id_personne is not null) src
on (tgt.login = src.login)
when matched then
update set tgt.id_personne = src.new_id_personne;

Related

Ways to update one table using another skipping null values

I have table1 and table2 (both have the same columns), I want to insert all records in table2 into table1 if they do not exists in table1, overwise I want to update table1's columns with all non NULL columns of table2.
I didnt manage to find a way to produce one sql that does both things.
As for the first part i manged with INSERT OR IGNORE.
The 2nd part is the problem.
This is what i have:
UPDATE main.datas
SET ot=(
SELECT ot FROM cards.datas
WHERE main.datas.id = cards.datas.id AND cards.datas.ot <> NULL
);
problem is, is that the whole (SELECT...) return NULL because of the cards.datas.ot <> NULL part. i tried doing cards.datas.ot IS NOT NULL instead but it just ignores it and sets NULL values as well.
Put the WHERE ... IS NOT NULL condition in the UPDATE statement:
UPDATE main.datas
SET ot = (SELECT ot FROM cards.datas WHERE main.datas.id = cards.datas.id)
WHERE (SELECT ot FROM cards.datas WHERE main.datas.id = cards.datas.id) is not null
or use coalesce():
UPDATE main.datas
SET ot=coalesce((
SELECT ot FROM cards.datas
WHERE main.datas.id = cards.datas.id AND cards.datas.ot IS NOT NULL
), ot);
The typical way to write this uses not exists:
UPDATE main.datas md
SET ot = (SELECT cd.ot
FROM cards.datas cd
WHERE md.id = cd.id AND cd.ot IS NOT NULL
)
WHERE NOT EXISTS (SELECT cd.ot
FROM cards.datas cd
WHERE md.id = cd.id AND cd.ot IS NOT NULL
);
This runs the risk of an error if the first subquery returns more than one row. For that reason, you might want to include LIMIT 1 or an aggregation function.

merge sql condition is null issue

I have two very similar SQL statements. On of them work and on not. The SQL error message seems to be misleading. Can you figure it out?
SQL 1 -- this works just fine
Merge into t1
Using (
Select art_ref_nr, channel, some_value From s1 ) t2
On ( t1.art_ref_nr = t2.art_ref_nr and t1.channel = t2.channel
and ( t1.datasource is null
or (t1.datasource = 'no such value' ) -- only null values will be found
))
WHEN MATCHED THEN UPDATE SET
t1.some_value = t2.some_value
, t1.datasource = 'value 1'
;
SQL 2 -- this fails
Merge into t1
Using (
Select art_ref_nr, channel, some_value From s1 ) t2
On ( t1.art_ref_nr = t2.art_ref_nr and t1.channel = t2.channel
and ( t1.datasource is null
))
WHEN MATCHED THEN UPDATE SET
t1.some_value = t2.some_value
, t1.datasource = 'value 2'
;
SQL1 runs fine.
SQL2 messages:
Columns referenced in the ON Clause cannot be updated: string Cause:
LHS of UPDATE SET contains the columns referenced in the ON Clause
On the other side I reference the on-clause "datasource", in both SQLs, so the error message cannot be full truth.
It seems like the problem is that one time I only check for null value entries. But why does this affect the SQL logic?
Many greetings,
Peter
My guess is that your first query doesn't produce an error because a matching row is never found.
For the second query, it has to do an UPDATE, but can't because you are refering the column to UPDATE into the ON clause.
To overcome this,try to move into a WHERE clause, the part of the ON clause refering the column(s) you are trying to UPDATE:
Merge into t1
Using (
Select art_ref_nr, channel, some_value From s1 ) t2
On (t1.art_ref_nr = t2.art_ref_nr and t1.channel = t2.channel)
WHEN MATCHED THEN UPDATE SET
t1.some_value = t2.some_value
, t1.datasource = 'value 2'
WHERE t1.datasource is null
;

PL/SQL Update Query

Attempting to update a table I have created with null values from another table I have created in PL/SQL:
I was able to utilize the below update in SQL Server but am running into issues within PL/SQL (dont ask why I am running it in both)
Overall_Inventory = Table created with some populated values and some null valuse; this is the table requiring updates to those null values
task_table = Table also created, but contains value needing to be updated into T1
update dbh.overall_inventory
set dbh.overall_inventory.case_due_date = tsk.TASK_ACTION_TIMESTAMP
from dbh.overall_inventory,
(SELECT tsk.INQ_KEY,
min(tsk.TASK_ACTION_TIMESTAMP) as TASK_ACTION_TIMESTAMP
FROM dbh.task_table tsk
inner join dbh.overall_inventory Inv
on tsk.INQ_KEY = inv.inq_key
where tsk.ACTION_CD = '324'
group by tsk.INQ_KEY
) tsk
where tsk.INQ_KEY = dbh.overall_inventory.inq_key`
Oracle doesn't support from clause in the update statement. In this situation merge statement can be used.
merge into overall_inventory oi
using (select tsk.inq_key,
min(tsk.task_action_timestamp) as task_action_timestamp
from task_table tsk
join overall_inventory Inv
on tsk.inq_key = inv.inq_key
where tsk.action_cd = '324'
group by tsk.inq_key) tsk
on (tsk.inq_key = oi.inq_key )
when matched then
update
set case_due_date = tsk.task_action_timestamp
where case_due_date is null -- as I understood only NULL values
-- need to be updated
Note: Not tested because no sample data and desired result were provided.
I think you're looking for update over a select:
UPDATE (
  SELECT product_id, category_id
  FROM product) st
SET st.category_id = 5
WHERE st.category_id = 4;

update rows from joined tables in oracle

I'm trying to migrate some tables into an existing table, I need to perform the updates only where DET_ATTACHMENT_ID equals DET_ATTACHMENT.ID, here's the query I have so far.
UPDATE DET_ATTACHMENT
SET attachment_type = 'LAB', -- being added by the query, to replace the table difference
payer_criteria_id = (
SELECT PAYER_CRITERIA_ID
FROM DET_LAB_ATTACHMENT
WHERE DET_LAB_ATTACHMENT.DET_ATTACHMENT_ID = DET_ATTACHMENT.ID)
WHERE exists(
SELECT DET_ATTACHMENT_ID
FROM DET_ATTACHMENT
JOIN DET_LAB_ATTACHMENT ON (ID = DET_ATTACHMENT_ID)
WHERE DET_ATTACHMENT_ID = DET_ATTACHMENT.ID
the problem with the existing query is that it's setting every row to have an attachment_type of "LAB", and nulling out the payer_criteria_id where it didn't match. What am I doing wrong?
The problem might be that your exists(...) predicate always evaluates to true, thus making the update run for all rows of det_attachment. Try it this way:
UPDATE DET_ATTACHMENT X
SET X.attachment_type = 'LAB',
X.payer_criteria_id = (
SELECT C.PAYER_CRITERIA_ID
FROM DET_LAB_ATTACHMENT C
WHERE C.DET_ATTACHMENT_ID = X.ID
)
WHERE
exists(
SELECT 1
FROM DET_ATTACHMENT A
JOIN DET_LAB_ATTACHMENT B
ON B.DET_ATTACHMENT_ID = A.ID
where B.det_attachment_id = X.id
)
;

UPDATE row when matching row exists in another table

I need to update a field on a table to be true only if a matching row exists in another table, for all the rows where the column is currently null in the main table.
This is a description of what I want to achieve:
UPDATE [LenqReloaded].[dbo].[Enquiry] A
SET [ResponseLetterSent] = 1
WHERE [ResponseLetterSent] IS NULL
AND EXISTS
(
SELECT * FROM [LenqReloaded].[dbo].[Attachment] B
WHERE A.[EnquiryID] = B.[EnquiryID]
)
This isn't syntactically correct.
I can't code it via an IF EXISTS... statement because I don't have the [EnquiryID] without reading the data from the table.
How should I format my UPDATE statement?
You weren't far off...
UPDATE A
SET A.[ResponseLetterSent] = 1
FROM [LenqReloaded].[dbo].[Enquiry] A
WHERE A.[ResponseLetterSent] IS NULL
AND EXISTS ( SELECT * FROM [LenqReloaded].[dbo].[Attachment] B WHERE A.[EnquiryID] = B.[EnquiryID] )
You need to use a join in your update:
UPDATE [LenqReloaded].[dbo].[Enquiry] SET [ResponseLetterSent] = 1
FROM [LenqReloaded].[dbo].[Enquiry] A
join [LenqReloaded].[dbo].[Attachment] B on A.[EnquiryID] = B.[EnquiryID]
WHERE A.[ResponseLetterSent] IS NULL
This seems counterintuitive, but you need to establish a table alias in a From clause but use that alias in the Update Clause...
Update E Set
ResponseLetterSent = 1
From LenqReloaded.dbo.Enquiry E
Where ResponseLetterSent Is Null
And Exists (Select * From LenqReloaded.dbo.Attachment
Where EnquiryID = E.EnquiryID)
The thing you are missing is the 'from' clause, which is a t-sql extension - it is the only way to assign an alias to the updated table
update [lenqreloaded].[dbo].[enquiry]
set [responselettersent] = 1
from [lenqreloaded].[dbo].[enquiry] a
where [responselettersent] is null
and exists (
select *
from [lenqreloaded].[dbo].[attachment] b
where a.[enquiryid] = b.[enquiryid]
)