【发布时间】:2021-05-14 21:13:12
【问题描述】:
我正在从 SQL Server 迁移到 PostgreSQL,并且在转换查询时遇到困难
我的 SQL Server 查询:
SELECT * FROM dbo.Stl_Mtr_userdetails AS table1 JOIN( SELECT urmo.urm_userid,
STUFF((SELECT ',' + urm.urm_role_name FROM dbo.STL_CS_Mtr_userrolemapping urm
WHERE urm.urm_userid = 'test2' AND urm.urm_status = 'A' AND urm.urm_appid = 'BCA'
FOR XML PATH('')),1,1,'') [user_roles],urmo.urm_appid FROM dbo.Stl_CS_Mtr_userrolemapping
urmo WHERE urmo.urm_appid = 'BCA' AND urmo.urm_userid = 'test2'
GROUP BY urmo.urm_userid,urmo.urm_appid) AS table2 ON
table1.ud_userid = table2.urm_userid WHERE (table1.ud_userid = 'test2')
PostgreSQL 查询
SELECT * FROM STL_Mtr_userdetails AS table1 JOIN( SELECT urmo.urm_userid,
query_to_xml('SELECT '','' || urm.urm_role_name FROM STL_CS_Mtr_userrolemapping urm WHERE
urm.urm_userid = ''test2'' AND urm.urm_status = ''A'' AND
urm.urm_appid = ''BCA''', true, false, '')
user_roles,urmo.urm_appid FROM STL_CS_Mtr_userrolemapping urmo
WHERE urmo.urm_appid = 'BCA' AND urmo.urm_userid = 'test2'
GROUP BY urmo.urm_userid,urmo.urm_appid) AS table2 ON
table1.ud_userid = table2.urm_userid WHERE (table1.ud_userid = 'test2')
上述转换后的 PostgreSQL 查询在执行时工作正常并产生以下结果
user_roles 字段。
<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<_x003F_column_x003F_>,role1</_x003F_column_x003F_>
</row>
<row>
<_x003F_column_x003F_>,role2</_x003F_column_x003F_>
</row>
</table>
我在从 xml 中提取值并将 user_roles 的输出作为 role1,role2 时遇到了困难。
请帮帮我。
【问题讨论】:
标签: sql-server postgresql database-migration string-aggregation