English 中文(简体)
如何建立 com式有限清单服务器?
原标题:What this query does to create comma delimited list SQL Server?

I ve written this query with the help of google to create a delimited list from a table but I didn t understand anything from this query.

任何人都能够向我解释所发生的事情。

 SELECT 
    E1.deptno, 
    allemp = Replace ((SELECT E2.ename AS  data()  
                       FROM emp AS e2 
                       WHERE e1.deptno = e2.DEPTNO 
                       FOR xml PATH(  )),    ,  ,  ) 
 FROM EMP AS e1 
 GROUP BY DEPTNO; 

Gives 我的结果

10  CLARK, KING, MILLER
20  SMITH, JONES, SCOTT, ADAMS, FORD
30  ALLEN, WARD, MARTIN, BLAKE, TURNER, JAMES
最佳回答

解释该守则的最简单方式是研究如何使用<代码>。 表格XML PATH用于实际XML。 缩略语 Employee:

EmployeeID      Name
1               John Smith
2               Jane Doe

你可以使用

SELECT  EmployeeID, Name
FROM    emp.Employee
FOR XML PATH ( Employee )

这将产生以下的XML:

<Employee>
    <EmployeeID>1</EmployeeID>
    <Name>John Smith</Name>
</Employee>
<Employee>
    <EmployeeID>2</EmployeeID>
    <Name>Jane Doe</Name>
</Employee>

PATH中删除了外壳标签,以便:

SELECT  Name
FROM    Employee
FOR XML PATH (  )

创造财富

    <Name>John Smith</Name>
    <Name>Jane Doe</Name>

当时你做的不是理想的,那一栏名称数据就造成了一种q错误,因为它试图制造一种并非法律标签的xml标签,因此产生了以下错误:

栏目数据(0x0028)载有XML表格所要求的无效XML识别符号;(0x0028)是第一个错误特征。

相关分局掩盖了这一错误,只是产生XML,没有标签:

SELECT  Name AS [Data()]
FROM    Employee
FOR XML PATH (  )

creates

John Smith Jane Doe

然后,你将空间替换成mas体,不言而喻。

If I were you I would adapt the query slightly:

SELECT  E1.deptno, 
        STUFF(( SELECT   ,   + E2.ename 
                FROM    emp AS e2 
                WHERE   e1.deptno = e2.DEPTNO 
                FOR XML PATH(  )
            ), 1, 2,   ) 
FROM    EMP AS e1 
GROUP BY DEPTNO; 

没有任何栏目将意味着不会产生xml的标签,在选定的电梯中添加 com号是指任何空间不会造成错误的名称。

ADDENDUM

详细论述KM在评论中所说的话,因为这似乎又增加了几个看法,逃避XML特征的正确方式是使用<代码>。

SELECT  E1.deptno, 
        STUFF(( SELECT   ,   + E2.ename 
                FROM    emp AS e2 
                WHERE   e1.deptno = e2.DEPTNO 
                FOR XML PATH(  ), TYPE
            ).value( . ,  NVARCHAR(MAX) ), 1, 2,   ) 
FROM    EMP AS e1 
GROUP BY DEPTNO; 
问题回答

逐步加以区别,从内。

<>Step 1:

2. 进行最棘手的询问,看它产生什么结果:

SELECT E2.ename AS  data()  
FROM emp AS e2 
WHERE e2.DEPTNO = 10
FOR XML PATH(  )

你们应当获得这样的产出:

CLARK KING MILLER

http://www.ohchr.org。

REPLACE 仅用取代空间,从而将产出转化为

CLARK, KING, MILLER

<>3>标准:

外部查询获得<代码> deptno数值——加上内部查询的结果——并产生您的最终结果。

<>SQL 页: 1 最近,我接过这个职位,转手了我的STUFF/FOR XML战略,以利用新的扼杀功能。 此外,还避免需要做JOIN/SUB RequestRY和XML(和奇编码问题)的间接费用,难以解释。

SELECT  E1.deptno, 
        STRING_AGG(E1.ename,  ,  ) AS allemp
FROM    EMP AS e1 
GROUP BY DEPTNO; 

<>>: 还须确保https://learn.microsoft.com/en-us/sql/t-sql/Functions/string-split-transact-sql”rel=“nofollow noreferer”>check out the对口STR_SPLIT<<<<>>>>>,使与有限数据一起工作变得更容易得多。

外部查询检索了一份部门编号清单,然后为每个部门设立分局,以归还属于该部的所有名字。 该次顺序使用表格XML表将产出格式化成单行的ma子清单。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签