On Mar 1, 2011, at 9:14 PM, eddy wrote:

> Hi All,
> 
> I have been trying to covert this sql query to sqlalchemy one for
> hours now with no luck. any help will be appreciated
> 
> 
> "update table1 set columnValue=(case when columnValue=A then B when
> columnValue=B then A end) where columnValue in (A,B);"
> 
> It is just swapping the  columnValue where its A it set B and where
> its B it set A

update uses values() for the SET part (docs: 
http://www.sqlalchemy.org/docs/core/tutorial.html#inserts-and-updates ), case 
is a little weird it takes a list of when/then pairs (docs + examples 
http://www.sqlalchemy.org/docs/core/expression_api.html?highlight=case#sqlalchemy.sql.expression.case
 )

from sqlalchemy.sql import table, column, case

table1 = table('t1', column('columnValue'))

print table1.update().\
    where(table1.c.columnValue.in_(['A', 'B'])).\
    values(columnValue=case([
        (table1.c.columnValue=='A', 'B'),
        (table1.c.columnValue=='B', 'A')
    ]
))



> 
> I tried
> query=update([table1.c.columnValue],case([table1.c.columnValue==A, B],
> [table1.c.columnValue==B, A]), where(table1.c.columnValue==A or
> table1.c.columnValue==B))
> 
> 
> ???
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to