This question already has answers here:
Update statement with inner join on Oracle
(15 answers)
Closed 5 years ago.
I have the Following Statement and need to create an update.
Select p.POS_NR, p1.VON_POS_NR
FROM table1 p. inner join table2 p1
ON p.Bestell_NR = p1.Vorgangs_NR
and p.Bestell_pos = p1.Vorgangs_pos
where NOT (p.POS_NR = p1.Von_Pos_NR)
Now I want to equal p.POS_NR and p1.Von_POS_NR, but I don't know how to create the update
I hope someone can help me
You can do this using a MERGE statement. I'll assume that TABLE1 has a primary key field named KEY_FIELD - you can substitute as necessary:
MERGE INTO TABLE1 t1
USING (SELECT p.KEY_FIELD, p.POS_NR, p1.VON_POS_NR
FROM TABLE1 p
INNER JOIN TABLE2 p1
ON p.BESTELL_NR = p1.VORGANGS_NR and
p.BESTELL_POS = p1.VORGANGS_POS
WHERE NOT (p.POS_NR = p1.VON_POS_NR) d
ON (d.KEY_FIELD = t1.KEY_FIELD)
WHEN MATCHED THEN
UPDATE
SET t1.POS_NR = d.VON_POS_NR;
Best of luck.
Related
This question already has answers here:
Update statement with inner join on Oracle
(15 answers)
Closed 7 months ago.
I am trying to update data from 1 table into another, however I don't know the structure required.
UPDATE ITEM_WMS iw
JOIN ITEM_CBO ic ON iw.ITEM_ID = ic.ITEM_ID
SET iw.critcl_dim_1 = ic.unit_length,
iw.critcl_dim_2 = ic.unit_height,
iw.critcl_dim_3 = ic.unit_width
WHERE ic.COLOUR_DESC = 'B4F';
That's wrong syntax for Oracle. MERGE might be a simpler (better?) option:
MERGE INTO item_wms iw
USING item_cbo ic
ON (ic.item_id = iw.item_id)
WHEN MATCHED
THEN
UPDATE SET
iw.critcl_dim_1 = ic.unit_length,
iw.critcl_dim_2 = ic.unit_height,
iw.critcl_dim_3 = ic.unit_width
WHERE ic.colour_desc = 'B4F';
If it has to be UPDATE, then:
UPDATE items_wms iw
SET (iw.critcl_dim_1, critcl_dim_2, critcl_dim_3) =
(SELECT ic.unit_length, ic.unit_height, ic.unit_width
FROM item_cbo ic
WHERE ic.item_id = iw.item_id
AND ic.colour_desc = 'B4F')
WHERE EXISTS
(SELECT NULL
FROM item_cbo ic1
WHERE ic1.item_id = iw.item_id
AND ic1.colour_desc = 'B4F');
(exists part is here to skip rows that shouldn't be updated; otherwise, you'd set those columns to null)
This question already has answers here:
Update statement with inner join on Oracle
(15 answers)
Closed 1 year ago.
I have some error when i tried to update on row in my database
update tbl_status
set document_status=11
from tbl_status
inner join tbl_log on tbl_status.id=tbl_log.id
where tbl_status.document_status=7 and tbl_log.IDS=662;
error :
SQL command not properly ended
any solution
In Oracle, that would be
update tbl_status a set
a.document_status = 11
where a.id = (select b.id --> possibly where a.id IN (select ...
from tbl_log b
where b.ids = 662
)
and a.document_status = 7;
Or, with merge:
merge into tbl_status a
using (select b.id
from tbl_log b
where b.ids = 662
) x
on (a.id = x.id)
when matched then update set
a.document_status = 11
where a.document_status = 7;
This question already has answers here:
Update a table using JOIN in SQL Server?
(13 answers)
Closed 6 years ago.
I have a simple select statement that identifies the rows I want to update. Basically i want to copy the vad_description to the vb_description and can't quite figure this out. Any Help would be appreciated.
SELECT
variant_bom.vb_id
,variant_bom.vb_description
,variant_detail.vad_description
FROM dbo.variant_bom
INNER JOIN dbo.variant_detail
ON variant_bom.vb_vad_id = variant_detail.vad_id
INNER JOIN dbo.variant_setting
ON variant_setting.vas_vad_id = variant_detail.vad_id
WHERE variant_setting.vas_manufactured_variant = 1
AND variant_setting.vas_discontinued_product = 0
Try something like this
UPDATE vb
SET vb.vb_description = vd.vad_description
FROM dbo.variant_bom vb
INNER JOIN dbo.variant_detail vd
ON vb.vb_vad_id = vd.vad_id
INNER JOIN dbo.variant_setting vs
ON vs.vas_vad_id = vd.vad_id
WHERE vs.vas_manufactured_variant = 1
AND vs.vas_discontinued_product = 0
Giving Alias name to tables will make your query more readable
This question already has answers here:
How do I create a table based on another table [duplicate]
(2 answers)
Closed 6 years ago.
everyone! I would like to save the table after I used INNER JOIN.
CREATE TABLE result AS (
SELECT measures.Day,
SELECT measures.Day,
stations.Name,
measures.name_measure,
measures_of_stations.Count,
measures.unit
FROM
measures_of_stations
INNER JOIN
stations
ON measures_of_stations.id_station = stations.id_station
INNER JOIN
measures
ON measures_of_stations.id_measure = measures.id_measure)
But I have an error "Invalid syntax next to ( ". Why?
Thank you in advance :)
You need to use the INTO clause, see example below:
SELECT measures.Day,
stations.Name,
measures.name_measure,
measures_of_stations.Count,
measures.unit INTO result
FROM
measures_of_stations
INNER JOIN
stations
ON measures_of_stations.id_station = stations.id_station
INNER JOIN
measures
ON measures_of_stations.id_measure = measures.id_measure
SELECT measures.Day,
stations.Name,
measures.name_measure,
measures_of_stations.Count,
measures.unit
INTO result
FROM
measures_of_stations
INNER JOIN
stations
ON measures_of_stations.id_station = stations.id_station
INNER JOIN
measures
ON measures_of_stations.id_measure = measures.id_measure
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Update a table using JOIN in SQL Server?
I am stuck on a very simple query, an update with a join, I want to put the value of the field RECORDTYPE in the RECORDTYPE field = to the value of TEMPLATETABLE.RECORDTYPE
I tried with this but I get continuous syntax errors, which is the problem?
update MAINTABLE MT
set MT.MYTYPE = TT.RECORDTYPE
inner join TEMPLATETABLE TT on TT.ID_RECORD_TEMPLATE = MT.ID_RECORD_TEMPLATE
You are missing the FROM clause. Try this instead:
UPDATE MT
SET MT.MYTYPE = TT.RECORDTYPE
FROM MAINTABLE MT
INNER JOIN TEMPLATETABLE TT
ON TT.ID_RECORD_TEMPLATE = MT.ID_RECORD_TEMPLATE