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

  • 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

  • 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[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • 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.

  • 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!

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply