I would like update the column [ACIdent] from the table [Values].
With the subquery i try to get an ID from the same table.
On the point ??? i try to access to the column [PathName] of the respective row of the update script. But this solution doesn't work.
How can i do this? Many thanks in advance!
Update [Values]
Set ACIdent =
(Select b.valueIdent From Values as b Where b.SampleIdent = 0 and b.PathName = ???Values.PathName)
Where ValueIdent= 614
Restructure your query a bit so you can alias the table.
Update v
Set ACIdent =
(Select b.valueIdent From Values as b Where b.SampleIdent = 0 and b.PathName = v.PathName)
From [Values] v
Where v.ValueIdent= 614
I'm using merge sql to find if the rows exist and then update or insert the data.
My problem is that if rows exist then update block works. But if not, there is no data inserted.
Is it because I use fixed data instead of datasource from q2?
Database version : oracle 9i
SQL:
merge into TBL_QTY q1
using (
select PROD_NO, PART_BATCH_NO, COND1_TYPE, BEG_DT, END_DT
from TBL_QTY
WHERE PROD_NO = 'A111'
AND PART_BATCH_NO = 'BAT0000171624'
AND COND1_TYPE = '172'
AND COND2_TYPE = 'XXX'
AND COND3_TYPE = 'XXX'
AND BEG_DT = '20201225'
AND END_DT = '20201225' ) q2
on
( q1.PROD_NO = q2.PROD_NO
and q1.PART_BATCH_NO = q2.PART_BATCH_NO
and q1.COND1_TYPE = q2.COND1_TYPE
and q1.BEG_DT = q2.BEG_DT
and q1.END_DT= q2.END_DT)
WHEN MATCHED THEN
update SET q1.OBTAIN_QTY = 15 ,
q1.SALE_QTY = 15
WHEN NOT MATCHED THEN
INSERT (PROD_NO, PART_BATCH_NO, COND1_TYPE, BEG_DT
,END_DT , OBTAIN_QTY , SALE_QTY )
VALUES('A111' ,'BAT0000171624' ,'172','20201225'
,'20201225', 17, 17)
The USING subquery selects values from the same table you want to insert into. This means it returns no rows when the values in your WHERE clause don't exist in that table.
Why doesn't that trigger the WHEN NOT MATCHED branch as you expect? Because you have an empty set: there is nothing for Oracle to match against, it's not even evaluated.
The way MERGE works is, we get a set of rows from the USING query and compare them with the contents of the target: the match is evaluated and the appropriate branch is triggered for each row in the USING set.
To make your code work you must have rows in the USING subsubquery. The normal way to do that is to select literals from DUAL. (Caution: the following code is free-styled and untested, so may contain syntax errors which are left as an exercise for the reader).
merge into TBL_QTY q1
using (
select 'A111' as prod_no
,'BAT0000171624' as part_batch_no
,'172' as cond1_type
,'20201225' as beg_dt
,'20201225' as end_dt
, 17 as obtain_qty
, 17 as sale_qty ) q2
on
( q1.PROD_NO = q2.PROD_NO
and q1.PART_BATCH_NO = q2.PART_BATCH_NO
and q1.COND1_TYPE = q2.COND1_TYPE
and q1.BEG_DT = q2.BEG_DT
and q1.END_DT= q2.END_DT)
WHEN MATCHED THEN
update SET q1.OBTAIN_QTY = 15
,q1.SALE_QTY = 15
WHEN NOT MATCHED THEN
INSERT (PROD_NO, PART_BATCH_NO, COND1_TYPE, BEG_DT
,END_DT , OBTAIN_QTY , SALE_QTY )
VALUES (q2.PROD_NO, q2.PART_BATCH_NO, q2.COND1_TYPE, q2.BEG_DT
,q2.END_DT , q2.OBTAIN_QTY , q2.SALE_QTY )
If you need to do this for multiple rows, you can use UNION ALL to weld together selects on DUAL in the USING subquery.
Which configuration can I set to get each clause of a WHERE statement to start on a new line? For example:
-- bad
select *
from table
where a = 1 and b = 2
-- good
select *
from table
where a = 1
and b = 2
In order to get each part of AND or OR condition on the next line, just set the following:
Queries / WHERE and HAVING clauses / Wrap elements ⟶ Chop
i am trying to get non matching records from two table by comparing some columns which are common in both tables.i am using sql query to get the result. my first table is snd_marketvisits this table have properties like id ,pjpCode , section code, popCode .pop_name and landmark similary my 2nd table have pjpcode , section code, popcode popname are common and there are some other fields.i want to get the names of the pop which are not in second table but present in snd_marketvisit table by comparing popcode, sectioncode and pjpcode in both tables.
SELECT *
FROM snd_marketvisits sm
LEFT JOIN snd_marketvisit_pops sp ON
sm.distributorCode = sp.distributor AND
sm.pjpCode = sp.pjp AND
sm.sectionCode = sp.sectionCode AND
sm.popCode = sp.popCode
WHERE
sm.sectionCode = '00016' AND
sm.pjpCode = '0001' AND
sm.distributorCode = '00190A'
It depends on the database, as far as I know, but if you ask for NULL inside your yoined fields you should get only the rows without a match.
SELECT *
FROM snd_marketvisits sm
LEFT JOIN snd_marketvisit_pops sp ON
sm.distributorCode = sp.distributor AND
sm.pjpCode = sp.pjp AND
sm.sectionCode = sp.sectionCode AND
sm.popCode = sp.popCode
WHERE
sm.sectionCode = '00016' AND
sm.pjpCode = '0001' AND
sm.distributorCode = '00190A'
AND sp.distributor IS NULL
How to update query in sql column by comparing two tables ? This might be duplicated question, but yet still cannot solve my problem. Any help would be appreciated.
What i've tried so far, but error
UPDATE b SET b.STAMP = b.STAMP + 10 FROM TB_FWORKERSCH b,TB_FWORKERCN a
WHERE a.ISSDATE>='20150401' AND a.UKEY=b.UKEY2 and b.STAMP=0 AND b.IG_SUMINS!=0
DB2 Database
DB2 doesn't allow a JOIN or FROM for an UPDATE clause (this is also not specified in the SQL standard).
You can achieve what you want with a co-related sub-query:
UPDATE tb_fworkersch b
SET stamp = stamp + 10
WHERE EXISTS (SELECT 1
FROM tb_fworkercn a
WHERE a.issdate >= '20150401'
AND a.ukey = b.ukey2)
AND b.stamp = 0
AND b.ig_sumins <> 0
Try this:
MERGE INTO TB_FWORKERSCH b
USING TB_FWORKERCN a
ON a.UKEY=b.UKEY2
AND a.ISSDATE>='20150401' AND b.STAMP=0 AND b.IG_SUMINS<>0
WHEN MATCHED
THEN UPDATE SET b.STAMP = b.STAMP + 10;