Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Uncertain JOIN condition - are there settings that control how it is tolerated? Expand / Collapse
Author
Message
Posted Saturday, October 19, 2013 10:43 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, August 15, 2014 8:26 PM
Points: 88, Visits: 126
Code below contains uncertainty:
JOIN #b on #a.id between #b.L1 and #b.L2

Second row in #a can be be assigned val=100 OR val=200 because 3 is between 1 and 10 as well as 3 is between 2 and 10
This uncertainty can be resolved by several means but I am happy to keep it as is; I don't care if the the val=100 or 200.

With my current settins the query runs OK.

Are there settings such that this query will raise an error?

Thanks,
FM
-------------------------------------

create table #a(
id int, val int)

INSERT #a(id) values(1)
INSERT #a(id) values(3)
INSERT #a(id) values(12)

select * FROM #a

create table #b (L1 int,L2 int, LVAL int)
INSERT #b(L1,L2,LVAL) values (1,10, 100)
INSERT #b(L1,L2,LVAL) values (2,10, 200)
INSERT #b(L1,L2,LVAL) values (10,20, 300)

select * FROM #b

update #a SET val=#b.LVAL
FROM #a JOIN #b on #a.id between #b.L1 and #b.L2

select * FROM #a


drop table #a
drop table #b
Post #1506437
Posted Sunday, October 20, 2013 4:51 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:18 AM
Points: 6,829, Visits: 13,290
I'm not sure what you mean by "settings", but one method you could use to make the update statement "fail" is to add a constraint:

ALTER TABLE #a WITH CHECK
ADD CONSTRAINT _check CHECK (id < 100) ;

Not sure, if this is what you're lokking for though...

As a side note: your statement I don't care if the the val=100 or 200 sounds scary to me...




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #1506465
Posted Sunday, October 20, 2013 5:31 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, August 15, 2014 8:26 PM
Points: 88, Visits: 126
Hi Lutz,

It seems I didn't make myself clear enough.
This is the case when the exact value used for an update is unpredictable: can be 100 or 200.
In this particalar SP this uncertainty is acceptable; that's what I meant by 'don't care' - nothing to be scared about and no need to modify the query.

My question is: if this query is run in a different environment, with different ANSI (or else) settings, is there a chance that the existanse of multiple possible values for the update would raise an error? I am only concerned about having an error; I am OK with unpredictable update result.

Cheers.

Post #1506467
Posted Monday, October 21, 2013 7:14 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 6:44 AM
Points: 29, Visits: 99
There are a couple of dynamics at work here:

A) Table order is never guaranteed unless you use an order by or some ordering/filtering mechanism like ROW_NUMBER() or such. So, the UPDATE will be affected by whatever order the matching rows come in.

B) As far as I can determine (and I would be happy to be proven wrong!), an UPDATE statement which matches a single output row with multiple input rows will choose the first non-NULL value it comes to. That is, if I could set it to a value of NULL, A, B, X, or CHEESE, then it will be set to A if it's ordered ascending, X if it's ordered descending, and potentially B or CHEESE if an order is not enforced. This sounds handy, but it's VERY DANGEROUS BEHAVIOR. If you're updating data, you should ALWAYS care about what value you're using. If you can't guarantee that you'll only ever get one match, then you either have a design problem, a problem with your query (not specific enough), or a data issue.

As far as I'm aware, the only place that this will raise an error is if you're doing this in a MERGE statement. The MERGE will (correctly, in my opinion) only allow you to UPDATE or DELETE a row once (obviously it would be silly to attempt to DELETE a row you've already deleted!) HOWEVER, "it doesn't raise an error" doesn't mean it's right!
Post #1506672
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse