Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Runaway UPDATE statement -- pls. help

Author  Topic 

steelkilt
Constraint Violating Yak Guru

255 Posts

Posted - 2002-11-26 : 10:07:38
Hi. I'm trying to update a key field -- I'll call it my anchor field -- in a table using the value in Field1 (see below) in the same table, ONLY after many criteria are met.

I have tried writing an UPDATE statement, which seems to work, but the odd thing is that I KNOW there are only a handful of updates to be made based on the many criteria I have established in the WHERE clause, and my UPDATE statement is listing over 200 records "affected" in Enterprise Manager.

When I check the records, the update seems to have worked, but, again, if SQL is telling me over 200 records have been affected, I know something is up. BTW, I'm testing this on a copy of the production table in order to iron out the bugs first.

The idea here is that Anchor_field should only be updated with Field1 values when the conditions in the following update statement are met:


UPDATE MAIN_TABLE

SET MAIN_TABLE.ANCHOR_FIELD = MAIN_TABLE.FIELD1

WHERE MAIN_TABLE.FIELD1 IS NOT NULL AND

MAIN_TABLE.FIELD1 <> '0'

AND

COALESCE (MAIN_TABLE.FIELD2,
MAIN_TABLE.FIELD3) IS NULL

OR

COALESCE (MAIN_TABLE.FIELD2,
MAIN_TABLE.FIELD3) = FIELD1



AND

MAIN_TABLE.ANCHOR_FIELD = '0' OR
MAIN_TABLE.ANCHOR_FIELD IS NULL


Here's an example of data in the table, followed by notes on what SHOULD happen if the UPDATE statement is written properly:


Ex. 1

Anchor F1 F2 F3

0 <NULL> 2 3

*in Ex. 1 above, anchor_field should NOT be updated with field1 value


Ex. 2

Anchor F1 F2 F3

0 1 1 1


*in Ex. 2 above, anchor_field SHOULD be updated with field1 value




Ex. 3

Anchor F1 F2 F3

<NULL> 2 <NULL> <NULL>

*in Ex. 3 above, anchor_field SHOULD be updated with field1 value


Any help is appreciated. I want to make sure I'm not doing an inadvertent updating! Also, I sense that the order of my key word operators, NOT NULL, AND, OR may be flaky. Any quick rule on the proper order of operators?



Edited by - steelkilt on 11/26/2002 10:09:14

mr_mist
Grunnio

1870 Posts

Posted - 2002-11-26 : 10:10:49
Because of your lack of brackets around the different parts of the conditions I would imagine that

OR

COALESCE (MAIN_TABLE.FIELD2,
MAIN_TABLE.FIELD3) = FIELD1

Is being evaluated as the only necessary matching condition. Bracket it in with whatever AND condition it should be associated with.

In fact you have an even worse one below

OR
MAIN_TABLE.ANCHOR_FIELD IS NULL

So your update will update any record where MAIN_TABLE.ANCHOR_FIELD IS NULL

-------
Moo.
Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2002-11-26 : 10:17:26
To make that more obvious

create table #moo
(thing int null)

insert into #moo values (1)
insert into #moo values (2)
insert into #moo values (3)

SELECT * from #moo where
thing = 1 and thing = 3 or 1 = 1

--This returns everything because 1 is always equal to one.

select * from #moo where
thing = 1 and (thing = 3 or 1 = 1)

--This returns just the row which matches thing = 1 AND (that either thing = 3 or 1 = 1)

drop table #moo

-------
Moo.
Go to Top of Page

steelkilt
Constraint Violating Yak Guru

255 Posts

Posted - 2002-11-26 : 10:47:16
Thanks for the tips. Can someone point me to a good resource that will show me actual examples of complex 'NOT/AND/OR' updates so I can see bracketed expressions in action?

thx

Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2002-11-26 : 11:03:39
Books on line has its own section on AND operators

If you search for AND operators and then go to

"Rules and Grid-Pane Conventions for Combining Search Conditions"

It explains them there.

-------
Moo.
Go to Top of Page

steelkilt
Constraint Violating Yak Guru

255 Posts

Posted - 2002-11-26 : 14:45:08
Thanks for all the help. I'd never tried the Grid pane before, and it was useful in showing me some bracketing conventions. I had to move the code into SQL editor, however, after the Grid pane became a pain. The big help was your tip re: checking BOL article, which I found online here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vdbt7/html/dvconaboutcombiningsearchconditionswithandor.asp

Now I've automated all the updates in a monster SPROC that works flawlessly and I'm going home early tonight!

Cheers, and thanks again.



Go to Top of Page
   

- Advertisement -