Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 query wierdness

Author  Topic 

sslyle
Starting Member

3 Posts

Posted - 2012-06-06 : 14:48:35
Tell me why this happens.

select *
from tbl_1 a
join tbl_2 b
on b.f1 = a.f2
-- returns 1031 rows

BUT

update a
set a.f2 = b.f2
from tbl_1 a
join tbl_2 b
on b.f1 = a.f1
-- returns 1029 rows

There are not any nulls in a.f1, b.f1, a.f2, nor b.f2

edit: moved to proper forum

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-06-06 : 14:54:13
You only have 1029 rows in tbl_1, but there are 1031 rows in tbl_2 that match on the f1 column in tbl1. Since you're updating tbl1 you'll only get its rowcount.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-06-06 : 16:23:57
What Rob said... Just to add a bit... When you do this sort of update you are updating the target with a random matching row from the source. I believe this is SQL failing to uphold the ANSI standard, but I could be wrong about that. In any event, if you were to use a MERGE statement you will find that it produces an error (rightfully so) as you are updating the same row more than once. The moral of teh story is that updating the same row more than once in a batch is bad. :)
Go to Top of Page

sslyle
Starting Member

3 Posts

Posted - 2012-06-06 : 16:47:59
I wish included these details.
tbl_1 has 1183 rows
tbl_2 has 1031 rows

All rows in tbl_2 have a match in tbl_1.
--
select * from tbl_2 where f1 in (select f1 from tbl_1) -> returns 1031 rows
--

f1 is a identifer (think about customer_id here). Each value in f1 in both tables are unique.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-06-06 : 17:35:05
quote:
All rows in tbl_2 have a match in tbl_1
Are you sure? Unless you have a foreign key that's enabled and trusted you can't guarantee that. See if this returns any rows:
SELECT f1 FROM tbl_2 EXCEPT SELECT f1 FROM tbl_1
Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-06-06 : 18:42:23
quote:
Originally posted by sslyle

I wish included these details.
tbl_1 has 1183 rows
tbl_2 has 1031 rows

All rows in tbl_2 have a match in tbl_1.
--
select * from tbl_2 where f1 in (select f1 from tbl_1) -> returns 1031 rows
--

f1 is a identifer (think about customer_id here). Each value in f1 in both tables are unique.





Hi
As you have already mentioned that table tbl_2 has 1031
this records are matched with tbl_1 records it means
tbl_1 has 1183 but 1031 records are matched or common
thats why your query returning total 1031 records from
your table .

Now talk about your UPDATE statement :


update a
set a.f2 = b.f2
from tbl_1 a
join tbl_2 b
on b.f1 = a.f1

Now this query updates tbl_1 on joining b.f1 = a.f1
and it is updating 1029 records .It means tbl_1 has
total 1029 records which are matched with tbl_2 which
has 1032 records.

Now Talk about your Select statement :
select *
from tbl_1 a
join tbl_2 b
on b.f1 = a.f2

Inner Join basically retrieve all common values
and here it is retrieving 1032 records which are
matched with 1029 records of tbl_1



Vijay is here to learn something from you guys.
Go to Top of Page

sslyle
Starting Member

3 Posts

Posted - 2012-06-06 : 20:53:59
Thanks guys.
You help me by prompting me to challenge an assumption I just simply ran with unquestioningly.

Found it: f1 in tbl_1 turns out to NOT be unique for values in the table - there are two values that are duplicates of other record in the same table.

f1s are not keys - but they are populated from an indentity column of a third table - hence my assumption.

None of these tables are associated with key relationships. So the applicaiton has violated the integrity of the table tbl_1.
Go to Top of Page
   

- Advertisement -