Sql server Update Query in table with duplicate records - sql-server-2012

I would like to know what happen when I do an update with duplicated rows, for example:
Table 1:
Email StatusID Status
phil#gmail.com NULL
dome#yahoo.es 1 Busy
phil#gmail.com 2 Online
dome#yahoo.es NULL
Table 2:
Email Name RejectionStatusID RejectionStatus
dome#yahoo.es Dome 1
phil#gmail.com Phil 2
Result?
Update Table2
SET RejectionStatusID = StatusID,
RejectionStatus = Status
FROM Table2 Inner Join Table1
ON Table2.Email = Table1.Email
I wouldlike to know which of the duplicated and why??? Not really sure!!! Of course I did the query and know what happen but why? Just want an explanation...
Thanks.
EDITED:
This is the example, this is what happen with null values!!!
http://sqlfiddle.com/#!6/6ee69/1/0

From BoL https://msdn.microsoft.com/en-us/library/ms177523(v=sql.110).aspx
The results of an UPDATE statement are undefined if the statement includes a FROM clause that is not specified in such a way that only one value is available for each column occurrence that is updated, that is if the UPDATE statement is not deterministic.

Related

SQL Update only updates one row

This code is only updating one row, why? It has to do with one of the sub-queries but I am not sure. I'm thinking the WHERE..IN in the UPDATE statement but I am not sure.
UPDATE [sde].[sy1].[Valve_evw]
SET [sde].[sy1].[Valve_evw].[MA]
= (SELECT [sde].[sy1].[Valve_Join_evw].[MC]
FROM [sde].[sy1].[Valve_Join_evw])
WHERE [sde].[sy1].[Valve_evw].[PrimaryKey]
IN (SELECT [sde].[sy1].[Valve_Join_evw].[PrimaryKey]
FROM [sde].[sy1].[Valve_Join_evw]
WHERE [sde].[sy1].[Valve_Join_evw].[MA]
!= [sde].[sy1].[Valve_Join_evw].[MC])
Context:
What I am trying to do is update the MA column in Valve_evw using the MC column in Valve_Join_evw. The PrimaryKey in Valve_evw references equivalent rows as the PrimaryKey in Valve_Join_evw. As in, a single row in Valve_Join_evw will have the same PrimaryKey as a single row in Valve_evw, thus that equivalency can be used to update the records in Valve_evw. Also the MA column is equivalent in both tables. [Note: The Valve_Join_evw table is created with ESRI mapping software using the spatial relationship between the Valve_evw and a separate table, this is how the duplicate rows exist]
I am using database views (hence the '_evw') in SQL Server with a default INSTEAD OF UPDATE trigger. This combination, views and trigger, prevents the use of table joins to do this update. I have also tried MERGE but that will not work either. Therefore I am stuck with the ANSI standard, hence the sub-queries. This script runs with no errors but it only updates a single row whereas there are about 9000 thousand rows in the tables.
The output message:
(1 row(s) affected)
(0 row(s) affected)
First of all let's reduce the eye hurting SQL to what it really is:
update sde.sy1.valve_evw
set ma = (select mc from sde.sy1.valve_join_evw)
where primarykey in (select primarykey from sde.sy1.valve_join_evw where ma <> mc)
WHERE clause
We look for all primarykey in valve_join_evw where a record's ma <> mc. We update all valve_evw records with such primarykey.
SET clause
For a record we want to update, we set ma to the value found with:
select mc from sde.sy1.valve_join_evw
But this query has no where clause, so what value does it select to fill the record's ma field? It selects all mc from valve_join_evw, so the DBMS probably picks one of these values arbitrarily. (It would be better, it raised an error.)
Conclusion
It is very easy to see which records the statement will update.
Which primarykey:
select primarykey from sde.sy1.valve_join_evw where ma <> mc
Which rows:
select *
from sde.sy1.valve_evw
where primarykey in (select primarykey from sde.sy1.valve_join_evw where ma <> mc)
As to the SET clause: Add a WHERE clause to your subquery that relates the record to select to the record to update (same ma? same primarykey?) E.g.:
set ma =
(
select mc
from sde.sy1.valve_join_evw vj
where vj.primarykey = valve_evw.primarykey
and vj.ma = valve_evw.ma
)
Hi there i recomend first to do the select statement and when you are ok with te records retrieved use the same where for the update statement
Here is what the final script looks like.
UPDATE [Valve_evw]
SET [Valve_evw].[MA] =
(
SELECT [Valve_Join_evw].[MC]
FROM [Valve_Join_evw]
WHERE[Valve_Join_evw].[PrimaryKey] = [Valve_evw].[PrimaryKey]
)
WHERE [Valve_evw].[PrimaryKey]
IN (
SELECT [Valve_Join_evw].[PrimaryKey]
FROM [Valve_Join_evw]
WHERE [Valve_Join_evw].[MA]
!= [Valve_Join_evw].[MC]
);

updating sql query value with select statement

I am trying to execute a query which is something like:
update table set column=(select column1 from table1);
I just want to store the value from other table to my column
but when i try my sql query it says
ERROR 1242 (21000): Subquery returns more than 1 row
definitely this means my table1 contains more than 1 row so i want to know that is there any way to store data into column from other table with multiple row.
or basically saving content of other table as a text something like
update table set column='Data in text from other table';
You probably need a correlation clause:
update table
set column = (select column1 from table1 where table.col = table1.col);
You need to decide what column(s) are used for the correlation.
it will work as i am getting your requirement.Please let me know if your requirement is other i ll make changes in query
update table_name t1
inner join table1 t2 on t1.id =t2.id
set column =column1
Your nested query returns multiple rows so you encountered this error.
Try in this way
UPDATE FirstTable
SET FirstTable.ColumnName =tbl2.ColumnName
FROM SecondTable tbl2 WHERE tbl2.Id = FirstTable.Id
There should be a common id or something that will help to find exact row.

Update the date fields from the values returned from a subquery Oracle SQL

I have an application that scans a table for IDs that do not have a processed date. When an ID is processed, a process date is added to Table 1 and the entire record is copied to another table (table 2) if it completes the application.
I need to compare IDs in the tables looking for IDs from table 1 that are not in table 2 but has a date for when it was processed. This is a sign that the id was processed but never made it to the end of the application and failed along the way.
I then need to update these IDs (records) from table 1 by setting the processing date to null so the application picks it up in the next run.
Here is a query that gets the IDs I need.
SELECT Subject_Number
From Table1
WHERE NOT EXISTS(SELECT NULL
FROM Table2
WHERE Table2.Subject_Number = Table1.Subject_Number)
AND Table1.Processed_Date IS NOT NULL.
Now I just need to update the processed date to null for the IDs this returns.
Any help will be greatly appreciated.
This query something like this
UPDATE <table> SET <fields> WHERE <table.id> IN (
SELECT Subject_Number
From Table1
WHERE NOT EXISTS(
SELECT NULL
FROM Table2
WHERE Table2.Subject_Number = Table1.Subject_Number)
AND Table1.Processed_Date IS NOT NULL.)

Writing a single UPDATE statement that prevents duplicates

I've been trying for a few hours (probably more than I needed to) to figure out the best way to write an update sql query that will dissallow duplicates on the column I am updating.
Meaning, if TableA.ColA already has a name 'TEST1', then when I'm changing another record, then I simply can't pick a value for ColA to be 'TEST1'.
It's pretty easy to simply just separate the query into a select, and use a server layer code that would allow conditional logic:
SELECT ID, NAME FROM TABLEA WHERE NAME = 'TEST1'
IF TableA.recordcount > 0 then
UPDATE SET NAME = 'TEST1' WHERE ID = 1234
END IF
But I'm more interested to see if these two queries can be combined into a single query.
I am using Oracle to figure things out, but I'd love to see a SQL Server query as well. I figured a MERGE statement can work, but for obvious reasons you can't have the clause:
..etc.. WHEN NOT MATCHED UPDATE SET ..etc.. WHERE ID = 1234
AND you can't update a column if it's mentioned in the join (oracle limitation but not limited to SQL Server)
ALSO, I know you can put a constraint on a column that prevents duplicate values, but I'd be interested to see if there is such a query that can do this without using constraint.
Here is an example start-up attempt on my end just to see what I can come up with (explanations on it failed is not necessary):
ERROR: ORA-01732: data manipulation operation not legal on this view
UPDATE (
SELECT d.NAME, ch.NAME FROM (
SELECT 'test1' AS NAME, '2722' AS ID
FROM DUAL
) d
LEFT JOIN TABLEA a
ON UPPER(a.name) = UPPER(d.name)
)
SET a.name = 'test2'
WHERE a.name is null and a.id = d.id
I have tried merge, but just gave up thinking it's not possible. I've also considered not exists (but I'd have to be careful since I might accidentally update every other record that doesn't match a criteria)
It should be straightforward:
update personnel
set personnel_number = 'xyz'
where person_id = 1001
and not exists (select * from personnel where personnel_number = 'xyz');
If I understand correctly, you want to conditionally update a field, assuming the value is not found. The following query does this. It should work in both SQL Server and Oracle:
update table1
set name = 'Test1'
where (select count(*) from table1 where name = 'Test1') > 0 and
id = 1234

How to match the longest string and update the value?

I need to compare and match the longest match of two strings in two different tables and update one values if there is a closest match.
Table 1 Table 2
stack1 stack2
ABCDEFG ABC
GHIJKLM ABCDE
PQRSUVW ABCDEF
I need to compare these two tables and match the closeet one and update Table 1 first row as ABCDEF the closest match, Please can anyone help me out. I am stuck here.
Here is my query
UPDATE table1 A
SET A.stack1 = (SELECT DISTINCT B.stack2
FROM table2 B
WHERE A.stack1 LIKE CONCAT(B.stack2,'%'))
WHERE name = 'name';
with this query am getting an error called
ORA-01427: single-row subquery returns more than one row
You need to make the subquery return only a single match (the longest one). In your case MAX should do that.
UPDATE table1 A
SET A.stack1 = (SELECT Max( B.stack2 )
FROM table2 B
WHERE A.stack1 LIKE CONCAT(B.stack2,'%'))
WHERE name = 'name';
Also, you should think about the case where nothing matches.
The ORA-01427 error is saying the subquery is returning more than one value -- even with the DISTINCT.
You need to correct the case(s) that are returning more than one distinct value. That could be with an aggregate function, like MAX or MIN, but without details I hesitate to make that recommendation.