SQLAlchemy:FOR XML PATH等价物

时间:2014-11-17 19:19:36

标签: sql sql-server sqlalchemy

如何在XML标记中放置包含多行的列?

这是我的示例mssql代码片段,

(SELECT DESCRIPTION 
 FROM WEV_STY PS WITH(NOLOCK) 
 JOIN MOEE_POV_STY MPS 
 WITH(NOLOCK) ON PS.STY_CD = MPS.PRV_STY_CD 
 FOR XML PATH('')) PPB_SDD

以上查询的sqlalchemy等价物是什么? 如何将 FOR XML PATH('')翻译成sqlalchemy?

1 个答案:

答案 0 :(得分:0)

您可以在SQLALCHEMY中执行原始SQL查询。根据{{​​3}}和SQLALCHEMY doc,尝试类似:

# pymssql
engine = create_engine('mssql+pymssql://scott:tiger@hostname:port/dbname')
raw_con = engine.raw_connection()

try:
    cursor = raw_con.cursor()

    # add your proper SQL code here
    cursor.execute('(SELECT DESCRIPTION 
                     FROM WEV_STY PS WITH(NOLOCK) 
                     JOIN MOEE_POV_STY MPS 
                     WITH(NOLOCK) ON PS.STY_CD = MPS.PRV_STY_CD 
                     FOR XML PATH('')) PPB_SDD;')

    results = cursor.fetchall()
    cursor.close()
finally:
    raw_con.close()