The results spliced into a string

#for xml path (param) - results of the query output in xml format

1 select id,name from table1 for xml path
2 --id and real field name is table1

- no argument following path, each row of data is the default <row> tag package, each row of data which is to be wrapped column name tag.

Sql above result may be as follows:

Copy the code
1 <row>
2     <id>1</id>
3     <name>holab</name>
4 </row>
5 <row>
6     <id>2</id>
7     <name>name1</name>
8 </row>
9 ....
Copy the code

 - each row of the outermost wrap label parameter controlled by the path (without parameters, defaults Row), thus can be removed directly to a path of the empty string as a parameter.

- Each data line is directly wrapped label control column names from the query results, to remove the need to ensure that only check out the column name is not listed on it (to ensure that no column name is the easiest way to add an empty each field string).
1 select (select id+'',name+'' from table1 for xml path(''))

result:

1 1holab2name1....

 #stuff function

stuff(character_expression,start,length,replaceWith_expression)
Refers to remove the starting position (start) of specified length (length) characters, and character insertion (replaceWith_expression) at this position instead of
 

#row_number () over (order by column) - Add the number row for the query results 

1 select id,name,row_number over(order by id) as num from table1

result:

1 id    name     num
2 1 1 holab
3 2     name1     2
4 4     name      3
5 ....

 # Query results spliced ​​into comma delimited string - Each data separated by a comma

1 select stuff((select ','+id+':',name+'' from table1 for xml path('')),1,1,'')

result:

1 1:holab,2:name1....

 # The results of each data line feed display (the beginning of each line to add number)

1 select (select row_number() over(order by id)+'、',id+':',name+char(10) from table1 for xml path(''))

result:

1 1.1: holab
2 2.2: name2
3 .... 
4 5 --char (10) for the line breaks in sql server
6 - because the last line also has a line break, so in the end will be more than one line blank line

 

Guess you like

Origin www.cnblogs.com/qiu18359243869/p/12204376.html