简体   繁体   English

'stuff' 和 'for xml path('')' 来自 Postgresql 服务器中的 SQL 服务器

[英]'stuff' and 'for xml path('')' from SQL Server in Postgresql

I'm migrating some SQL Server 2008R2 queries to Postgresql 9.0 and I have some trouble with it.我正在将一些 SQL Server 2008R2 查询迁移到 Postgresql 9.0,但我遇到了一些问题。 Here's the SQL Server query:这是 SQL 服务器查询:

stuff((select ', '+p.[NAME] as 'data()' 
from BPROVIDERS_PROVIDER p, BORDER_ARTICLEORDERPROVIDER aop 
where p.OID = aop.PROVIDER for xml path('')),1,1,'')) as pNAMES

Reading SQL Server documentation I understand that this creates a comma separated list.阅读 SQL 服务器文档我知道这会创建一个逗号分隔的列表。 I think that I can change stuff function to overlay function in Postresql'.我认为我可以更改stuffoverlay Postresql 中的 function。 Am I correct?我对么?

The second problem comes with SQL Server's for xml path with ('') as a parameter.第二个问题来自 SQL 服务器的for xml path ,以 ('') 作为参数。 It returns the values assigned to an attribute called pNAMES instead of create row elements.它返回分配给名为pNAMES的属性的值,而不是创建行元素。 Is that correct?那是对的吗?

Does Postgresql Query_to_xml() function with attribute tableforest = 'true' do the same?属性 tableforest tableforest = 'true'的 Postgresql Query_to_xml() function 会做同样的事情吗?

Thank you.谢谢你。

You can use string_agg instead.您可以改用string_agg

SQL Fiddle SQL小提琴

PostgreSQL 9.1.6 Schema Setup : PostgreSQL 9.1.6 架构设置

create table T
(
  Name varchar(10)
);

insert into T values('Kalle');
insert into T values('Pelle');
insert into T values('Urban');

Query 1 :查询 1

select string_agg(Name, ',') as Names
from T

Results :结果

|             NAMES |
---------------------
| Kalle,Pelle,Urban |

You can replace it by String_agg for example SQL Server stored procedure:您可以用 String_agg 替换它,例如 SQL Server 存储过程:

STUFF((SELECT DISTINCT ', ' + CONVERT(VARCHAR,L.ROLE_SUB_CAT_ID) 
       FROM [PHS].[dbo].PHS_ADMIN_USER_ACCESS_DTL K,
            [PHS].[dbo].[PHS_ADMIN_USER_ROLE_SUB_CAT_MST] L
       WHERE L.ROLE_SUB_CAT_ID = K.ROLE_SUB_CAT_ID 
         AND K.UMC_ID = A.UMC_ID 
         AND K.CCR_ID = A.CCR_ID
       FOR XML PATH('')), 1, 1, '') AS ROLE_SUB_CAT_ID

Convert it to postgresql like this:将其转换为 postgresql,如下所示:

string_agg((SELECT distinct ', ' ||  cast(L.ROLE_SUB_CAT_ID as VARCHAR) FROM PHS.dbo.PHS_ADMIN_USER_ACCESS_DTL K,
          PHS.dbo.PHS_ADMIN_USER_ROLE_SUB_CAT_MST L
          WHERE L.ROLE_SUB_CAT_ID = K.ROLE_SUB_CAT_ID AND K.UMC_ID = A.UMC_ID AND K.CCR_ID=A.CCR_ID
          ), 1, 1, '') AS ROLE_SUB_CAT_ID

STUFF() with XML PATH带有 XML 路径的 STUFF()

same teble to record STUFF记录 STUFF 的同一个桌子

SELECT distinct C.country, X.ProductList FROM     
tbl_demo as C     
CROSS APPLY    
(    
SELECT STUFF    
      (    
          (    
              SELECT 
                    distinct 
                        ',' + P.product 
                    FROM 
                        tbl_demo AS P    
                    JOIN 
                        tbl_demo AS CP 
                    ON 
                        P.country = CP.country     
                    WHERE 
                        CP.sub_id = C.sub_id    
                    FOR XML PATH('')    
          )    
              
      ,1,1,'') as ProductList    
) as X

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM