Update with IN condition

Hi,

i want to run this update script with in where clause an IN condition.
I want to make only one call at database. I don’t want to cycle on each value of the array.
This is my script:

system.db.runPrepUpdate("UPDATE TEST SET FL= 1 WHERE ID IN (?)", [y], "dscada_main")

The y variable is an array of int value such as:
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

But I have this error:
caused by GatewayException: SQL error for “UPDATE TEST SET FL= 1 WHERE ID IN (?)”: The conversion from UNKNOWN to UNKNOWN is unsupported.
caused by SQLServerException: The conversion from UNKNOWN to UNKNOWN is unsupported.

Can I solve it?

Thanks
Andrea

Hi Andrea,

By array, do you mean a Python list? This will be my assumption unless you tell me differently.

I’d say the problem is that SQL doesn’t understand lists. Try converting the list into a string:

system.db.runPrepUpdate("UPDATE TEST SET FL= 1 WHERE ID IN (?)", [str(y)[1:-1]], "dscada_main")

Not that converting a list to string keeps the brackets, so the [1:-1] will remove them.
[attachment=0]2016-05-17_8-14-43.png[/attachment]

You cannot pass a list into a single ‘?’ substitution parameter. Jordan’s suggestion would be suitable in your case if you use ‘%s’ instead of ‘?’ and use the non-Prep form of the query.
When using data types in a list that would be problematic when converted to a string, you must place one ‘?’ for each element of list. Like so:system.db.runPrepUpdate("UPDATE TEST SET FL= 1 WHERE ID IN (%s)" % (", ".join(['?'] * len(y))), y, "dscada_main")

1 Like