Home Forums SQL Server 2008 SQL Server 2008 - General Uncertain JOIN condition - are there settings that control how it is tolerated? RE: Uncertain JOIN condition - are there settings that control how it is tolerated?

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