SQL query result will be stitched together into a string string search result sqlserver

Results will be spliced ​​into a string sqlserver

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

The above mentioned id the SELECT 1, name from table1 for xml path 
2 --id and name for the real field of 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     holab     1
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) is in line feed sql server 
6 - as the last line has line break, the final row will be more blank lines
#for xml path (param) - results of the query output in xml format

The above mentioned id the SELECT 1, name from table1 for xml path 
2 --id and name for the real field of 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     holab     1
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) is in line feed sql server 
6 - as the last line has line break, the final row will be more blank lines

Guess you like

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