tags:

views:

623

answers:

4

I'm trying to write a query that updates rows in a table if a certain column has a value in a list I'm providing:

UPDATE MY_TABLE SET COL1 = 'xyz' WHERE COL2 IN ('x', 'y', 'z');

I'm getting a syntax error, but I know that this should be possible. It's essentially a single command to execute the following 3 commands:

UPDATE MY_TABLE SET COL1 = 'xyz' WHERE COL2 = 'x';
UPDATE MY_TABLE SET COL1 = 'xyz' WHERE COL2 = 'y';
UPDATE MY_TABLE SET COL1 = 'xyz' WHERE COL2 = 'z';

The values xyz are being set dynamically by the user, and there could be an arbitrary number of values (or I would just code it the long and awful way and be done with it. The only information I can find on the IN clause is concerned with subqueries. Can someone help me rewrite this query?

Many thanks.

A: 

Is that your full update query? It looks like it is missing the end part. Short of the missing bits, it looks fine. Can you send the actual error message you receive?

Beau Simensen
Looks like an iPhone rendering problem. Will check on actual computer later!
Beau Simensen
yeah, that's the full query, and I'm not sure what's missing.
Elie
A: 

I am pretty sure that you do not need to quote the individual values.

UPDATE MY_TABLE SET COL1 = 'xyz' WHERE COL2 IN ('x, y, z');

This always gets me too.

And if the values are numeric, you don't need quotes at all.

Toby Hede
I think you do need the individual quotes.
Otávio Décio
I tried with and without quotes, and it still doesn't work. While the column is numeric, as far as I know the quotes don't hurt.
Elie
wait... if the column is numeric, you *cannot* compare to the literals x, y z, only to numbers 1,2 3 etc
Otávio Décio
'x, y, z' is a single string value. This is syntactically correct, but probably not what the OP intended. If you want to compare to three individual strings, you must quote each of them separately.
Bill Karwin
Yeah, my bad. I didn't mean to use the alpha literals, but you could use numbers without quotes if the actual data was numeric (as opposed to the sample provided). Also: you *totally* need to quote string literals. I need more coffee :P
Toby Hede
+2  A: 

You should post the precise error message. The error message will give a clue about what part of the query confused the parser.

Do some of the values x, y, z contain quotes as part of the value? You could have imbalanced quotes. For example the following is clearly a syntax error:

UPDATE MY_TABLE SET COL1 = 'xyz' WHERE COL2 IN ('O'Reilly', 'Smith', 'Jones');

Give some more information and I'll edit this answer with more troubleshooting suggestions.

Bill Karwin
I am curious if this example actually fails for these exact values as well because it looks good to me. A case like you are suggesting here ( with O'Reilly not properly escaped ) is about the only thing I can think of.
Beau Simensen
there are no extra quotes... I checked the query, and they are all balanced. All values are numeric, so no internal quotes to throw this off.
Elie
+1  A: 

So now that everyone is telling me to look for unbalanced quotations, I looked at the query more carefully, and found the following:

UPDATE BOOK SET INVOICE_ID TO '1' WHERE START_NUMBER IN (1)

which should be:

UPDATE BOOK SET INVOICE_ID = '1' WHERE START_NUMBER IN (1)

Thanks for all the help, though, but now I feel nice and dumb.

Elie