Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Uncertain JOIN condition - are there settings that control how it is tolerated?


Uncertain JOIN condition - are there settings that control how it is tolerated?

Author
Message
philmond
philmond
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 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
LutzM
LutzM
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7007 Visits: 13559
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
philmond
philmond
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 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.
sqlslacker
sqlslacker
Valued Member
Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)Valued Member (53 reputation)

Group: General Forum Members
Points: 53 Visits: 209
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!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search