October 19, 2013 at 10:43 pm
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
October 20, 2013 at 4:51 am
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...
October 20, 2013 at 5:31 am
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.
October 21, 2013 at 7:14 am
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