I am trying to update my master table from my updates table, What it wrong with the below query.
UPDATE master_table
SET master_table.description = master_table_import.description
FROM master_table_import
WHERE master_table.user_id = master_table_import.user_id
You are missing a join between the tables. Try something like this:
UPDATE mt SET mt.Description = mti.Description
FROM master_table mt
INNER JOIN master_table_import mti
ON mt.user_id = mti.user_id;
It is always good idea to use aliases for tables. To update you have join your target table with source table:
UPDATE mt
SET description = mti.description
FROM master_table mt INNER JOIN master_table_import mti
WHERE mt.user_id = mti.user_id
Related
I have table that called hanpaka there I want to update the cardNum from temp table the connection between the two is IDMember and MemberId. but updating like this cause multiple cardNum not to the right MemberId why?
UPDATE Knowledge4All..Hanpaka
SET CardNum = (c.CardNumber )
FROM #Temp2 c inner join Hanpaka h on IDMember = h.MemberId`
Try using the alias in the update:
UPDATE h
SET CardNum = c.CardNumber
FROM #Temp2 c JOIN
Hanpaka h
ON c.IDMember = h.MemberId;
SQL Server does allow the table to be repeated in the update. However, it might get confused and your query might be doing a Cartesian product.
We have three tables here, Session table which is connected to Green_fact table with Session_id field. Time_session table is the third table which is made of SessionDate from Session table with a primary key for every single day.
How can I populate the Date_ID field in Green_fact table.
the code below is what I think use, but it doesn't work properly
update green_fact
inner join "SESSION" on green_fact."SESSION_ID" = "SESSION"."SessionID"
inner join "TIME_SESSION" on "TIME_SESSION"."SESSION_DATE" = "SESSION"."SessionDate"
set green_fact."DATE_ID" = "TIME_SESSION"."ID" where green_fact."SESSION_ID" = "SESSION"."SessionID";
Oracle doesn't allow join in update. You can use correlated subqueries:
update green_fact gf
set DATE_ID = (select ts.ID
from SESSION s join
TIME_SESSION ts
on ts.SESSION_DATE = s.SessionDate
where gf.SESSION_ID = s.SESSIONID
)
where exists (select ts.ID
from SESSION s join
TIME_SESSION ts
on ts.SESSION_DATE = s.SessionDate
where gf.SESSION_ID = s.SESSIONID
);
The exists may not be necessary, if all the rows match.
In Oracle you can either update a table or an updatetable query, i.e. UPDATE tablename SET ... or UPDATE (SELECT ... FROM ...) SET ....
update
(
select gf.date_id, time_session.id as time_session_id
from green_fact gf
inner join session s on gf.session_id = s.sessionid
inner join time_session ts on ts.session_date = s.sessiondate
)
set date_id = time_session_id;
This will work, provided the DBMS sees it guaranteed that the query produces one row only per green_fact record (which it should, because of the primary and foreign keys).
Use below query for update from two tables :
UPDATE green_fact SET green_fact."DATE_ID" = A.Id
FROM
(
SELECT "TIME_SESSION"."ID" Id , "SESSION"."SessionID" SessionID
FROM "TIME_SESSION"
JOIN "SESSION" ON "TIME_SESSION"."SESSION_DATE" =
"SESSION"."SessionDate"
) A
WHERE green_fact."SESSION_ID" = A.SessionID;
Oracle doesn't allow joins to be used in UPDATE statements, but sometimes one can rewrite such a statement as a MERGE (you don't specify the version of Oracle you're using, but since 10g one can omit the WHEN MATCHED or WHEN NOT MATCHED clauses of the MERGE):
MERGE INTO green_fact gf
USING (
SELECT s."SessionID", ts.session_date, ts.id
FROM session s INNER JOIN time_session ts
ON s."SessionDate" = ts.session_date ) ts1
ON ( gf.session_id = ts1."SessionID" )
WHEN MATCHED THEN
UPDATE
SET gf.date_id = ts1.id;
Hope this helps.
By the way, I cannot stress enough that mixed-case object names in Oracle are a bad idea. But maybe you're dealing with legacy data and don't have a choice.
I haved saved SELECT query. I need create update query to update table field with value from saved select query.
Im getting error "Operation must use an updatable query".
Problem is that saved select query result not contain primary key.
UPDATE [table] INNER JOIN
[saved_select_query]
ON [table].id_field = [saved_select_query].[my_field]
SET [table].[target_field] = [saved_select_query]![source_field]);
Im also try with select subquery instead of inner join, but same error.
Perhaps a DLookUp() will do the trick:
UPDATE [table] SET
[target_field] = DLookUp("source_field", "saved_select_query", "my_field=" & id_field)
... or, if the joined field is text ...
UPDATE [table] SET
[target_field] = DLookUp("source_field", "saved_select_query", "my_field='" & id_field & "'")
I'm not sure I completely understand what you are asking.
If you are asking what syntax to use when performing an update with an inner join.
UPDATE tableAlias
SET Column = Value
FROM table1 as tableAlias
INNER JOIN table2 as table2Alias on tableAlias.key = table2Alias.key
your query is incorrect , try this.
UPDATE [table]
SET [table].[target_field] = [table2].
[source_field])
from (select *from
[table] INNER JOIN
[saved_select_query] a
ON [table].id_field =
a.[my_field] )
table2
I got it to work using the following:
UPDATE [table]
SET [table].[target_Field] = (SELECT [source_field] FROM [Saved_Select_Query]
WHERE [table].[id_field] = [Saved_Select_Query].[my_field])
You can't use an JOIN on an UPDATE statement directly, so you need to join the tables in a subquery.
I am trying to run the below update but running into the "table is ambiguous" error.
UPDATE dbo.cg
SET cg.column = gId.ID
FROM dbo.a
INNER JOIN dbo.cg as cId ON cId.[a] = dbo.a.[c]
INNER JOIN dbo.cg as gId ON gId.[a] = dbo.a.[b];
The table dbo.a contains data to update a value in cg based on a relationship to same table against a value in a different column. It is a self-referencing hierarchy.
As you can see, everything is aliased so I am a bit confused why this won't run.
Many thanks in advance for any help that can be provided.
In SQL Server, you should use the alias in the update, not the table. In addition, you have no alias called cg. So something like this:
UPDATE cId
SET column = gId.ID
FROM dbo.a a INNER JOIN
dbo.cg cId
ON cId.[a] = a.[c] INNER JOIN
dbo.cg gId
ON gId.[a] = a.[b];
Not to worry, solved it by luck.
I inner joined the table to itself in desperation ...
UPDATE dbo.cg
SET cg.column = gId.ID
FROM dbo.a
INNER JOIN dbo.cg as cId ON cId.[a] = dbo.a.[c]
INNER JOIN dbo.cg as gId ON gId.[a] = dbo.a.[b]
INNER JOIN cg ON cId.[a] = cg.[a];
If anyone could explain why that has worked, I would really appreciate understanding the MS SQL logic underneath.
I am writing a script to update the duplicate contact and all its tables where it is referenced.
One of the update statements that I have is the following:
/* update the contactid, and the compcontactid on the compcontact table */
UPDATE cmpc
SET cmpc.contactid = tt.contactid,
cmpc.compcontactid = (SELECT MAX(cc.compcontactid)
FROM compcontact cc
INNER JOIN #tempDupContacts tdup ON tdup.contactid = cc.contactid
INNER JOIN #tempTable tt ON tt.namefml = tdup.namefml)
FROM compcontact cmpc
INNER JOIN #tempDupContacts tdup ON tdup.contactid = cmpc.contactid
INNER JOIN #tempTable tt ON tt.namefml = tdup.namefml
However, when I run the script (script is way tooo long to post here), I get the following error:
Msg 2601, Level 14, State 1, Line 255
Cannot insert duplicate key row in object 'dbo.compcontact' with unique index 'XPKcompcontact'. The duplicate key value is (A000UZCU, A00JTCAP, X00GM2NF).
Can anyone explain why this is happening, and what the fix would be?
Is this because It is attempting to update the value that has the
You are attempting to update a unique key value to a value that already exists in the column. This is not allowed, since each value in the unique key must be unique.
As #sion_corn said, you're trying to update a column having unique key constraint.
Try adding a WHERE clause in your update statement to exclude the value which already exists.
For example:
UPDATE cmpc
SET cmpc.contactid = tt.contactid,
cmpc.compcontactid = (SELECT MAX(cc.compcontactid)
FROM compcontact cc
INNER JOIN #tempDupContacts tdup ON tdup.contactid = cc.contactid
INNER JOIN #tempTable tt ON tt.namefml = tdup.namefml
WHERE cc.compcontactid <> cmpc.compcontactid)
FROM compcontact cmpc
INNER JOIN #tempDupContacts tdup ON tdup.contactid = cmpc.contactid
INNER JOIN #tempTable tt ON tt.namefml = tdup.namefml
WHERE cmpc.contactid <> tt.contactid