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
Related
I am trying to do an SQL UPDATE query to set a value for b.[Disposition] WHERE the i.uid field is unique
The following select statement returns the correct rows.
Select distinct i.*
FROM [dbo].[Imported] i
inner join [DaisyCompare].[dbo].[Baseline] b
on b.[CLI] = i.[CLI]
and b.[Quantity] = i.[Quantity]
and b.[UnitCost] = i.[UnitCost]
and b.[TotalCost] = i.[TotalCost]
and b.[Description] = i.[Description]
However I am unsure how to incorporate that into an SQL UPDATE statement.
Any help greatly appreciated.
Try this
UPDATE upb
SET b.Disposition = "YOUR VALUE"
FROM [DaisyCompare].[dbo].[Baseline] ubp
INNER JOIN (Select distinct i.* FROM [dbo].[Imported] i
inner join [DaisyCompare].[dbo].[Baseline] b on
b.[CLI]=i.[CLI] AND
b.[Quantity]=i.[Quantity] AND
b.[UnitCost]=i.[UnitCost] AND
b.[TotalCost]=i.[TotalCost] AND
b.[Description]=i.[Description] )tmp ON Tmp.UID = ubp.UID
Your question is not very clear but you can do something like this
update b
set -- your fields here
FROM [DaisyCompare].[dbo].[Baseline] b
inner join [dbo].[Imported] i on
b.[CLI]=i.[CLI] AND
b.[Quantity]=i.[Quantity] AND
b.[UnitCost]=i.[UnitCost] AND
b.[TotalCost]=i.[TotalCost] AND
b.[Description]=i.[Description]
I think this works..Mention the column name equal to
update b
set b.disposition = i.xxxx from [dbo].[Imported] i
inner join [DaisyCompare].[dbo].[Baseline] b on
b.[CLI]=i.[CLI] AND
b.[Quantity]=i.[Quantity] AND
b.[UnitCost]=i.[UnitCost] AND
b.[TotalCost]=i.[TotalCost] AND
b.[Description]=i.[Description]
update [b].[Disposition] set x(Desired value)
from [dbo].[Imported] i inner join [DaisyCompare].[dbo].[Baseline] b on
b.[CLI]=i.[CLI] AND
b.[Quantity]=i.[Quantity] and
b.[UnitCost]=i.[UnitCost] and
b.[TotalCost]=i.[TotalCost] and
b.[Description]=i.[Description] where i.uid = y(Desired value)
I have an update to process on a trigger when a specific code in the column is input. When I reference the inserted table, which shows in the intellisence dropdown list when I type inserted I get the error:
The multipart identifier "inserted.contact" could not be bound.
Any idea how to reference a column in the inserted table for a trigger? Also, why is it better to use the inserted table than the existing table?
USE PCUnitTest
GO
UPDATE P
SET
P.First_Name = T.ForeName
,P.Middle_Name = T.Middle_Name
,P.Last_name = T.Surname
FROM GMUnitTest.dbo.Contact1 C
INNER JOIN PCUnitTest.dbo.People P
ON P.People_ID = C.Key4
CROSS APPLY dbo.NameParser(inserted.Contact) T --"the multipart identifier "inserted.contact" could not be bound"
WHERE C.Key1 = '76'
;
You bring in inserted just like any table:
UPDATE P
SET P.First_Name = T.ForeName,
P.Middle_Name = T.Middle_Name,
P.Last_name = T.Surname
FROM PCUnitTest.dbo.People P JOIN
GMUnitTest.dbo.Contact1 C
ON P.People_ID = C.Key4 JOIN
inserted i
ON i.?? = c.?? CROSS APPLY
dbo.NameParser(i.Contact) T
WHERE C.Key1 = '76';
The only question is what columns you use for the JOIN. The ?? are for your column names.
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
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.
What is wrong with the following PostgreSQL query?
UPDATE project_project SET project_project.create_date = assignments.start_date
FROM project_project
LEFT JOIN account_analytic_account ON account_analytic_account.id = project_project.analytic_account_id
LEFT JOIN assignments ON assignments.accounts_ref = account_analytic_account.id
gives
[SQL] UPDATE project_project SET project_project.create_date = assignments.start_date
FROM project_project
LEFT JOIN account_analytic_account ON account_analytic_account.id = project_project.analytic_account_id
LEFT JOIN assignments ON assignments.accounts_ref = account_analytic_account.id
[Err] ERROR: table name "project_project" specified more than once
I tried,
UPDATE pp SET pp.create_date = am.start_date
FROM project_project as pp
LEFT JOIN account_analytic_account as aa ON aa.id = pp.analytic_account_id
LEFT JOIN assignments as am ON am.accounts_ref = aa.id
gives
[SQL] UPDATE pp SET pp.create_date = am.start_date
FROM project_project pp
LEFT JOIN account_analytic_account aa ON aa.id = pp.analytic_account_id
LEFT JOIN am ON am.accounts_ref = aa.account_analytic_account.id
[Err] ERROR: relation "pp" does not exist
LINE 1: UPDATE pp SET pp.create_date = am.start_date
What is the correct syntax?
There are two problems to avoid:
1- in SET column=value, do not prepend a table name or alias to column. It yields an error and is useless anyway, since UPDATE tablename is only able to update columns from tablename. An alias of tablename can be used in other places, but not in the SET clause.
2- don't repeat the name of the table to update in the rest of the query unless you want to specifically induce a secondary and uncorrelated scan of this table.
Here is my proposal of a modified query that I hope does what you intend:
UPDATE project_project pp SET create_date =
(select assignments.start_date FROM account_analytic_account LEFT JOIN assignments
ON assignments.accounts_ref = account_analytic_account.id
WHERE
account_analytic_account.id = pp.analytic_account_id);