SqlServer Stuff

SqlServer Stuff 


the DECLARE  @TAB  TABLE 
( 
       the UserID the INT , 
       UserName NVARCHAR ( 50 ) 
) 
the INSERT  the INTO  @TAB 
( 
    the UserID, 
    UserName 
) 
the VALUES 
( 0 , N ' Simon catering cod ' ), 
( . 1 , N ' night employed by ' ), 
( 2 , N ' Linghu ' ), 
( . 3 , N ' soldier A ' ), 
(. 4 , N ' Hu learn ' ), 
( . 5 , N ' Wudaokou ' ), 
( . 6 , N ' six crossing ' )
 - The purpose of the user name with a delimiter ',' spaced character came to the conclusion: "Simon. cooking cod, night-employed multiply, Linghu, soldiers, armor, Hu science, Wudaokou, six crossing " 
- the final statement Sql first glance, a bit ignorant, one step below unbundled said 
the SELECT  STUFF 
( 
( the SELECT  ' , ' + UserName the FROM  @TAB  the FOR the XML the PATH ( '' )),
 . 1 ,
 . 1 ,
 '' 
) 
- the SELECT * the FROM @TAB 
-The PATH the XML the FOR ( '') 
the SELECT UserName the FROM  @TAB  the FOR the XML the PATH ( '' )
 - the FOR the XML the PATH ( '') of the add separator ',' 
the SELECT  ' , ' + UserName the FROM  @TAB  the FOR the XML the PATH ( ' ' )
 - Stuff the first delimiter', 'taken 
the SELECT  STUFF is 
( 
( the SELECT  ' , ' + UserName the FROM  @TAB  the FOR the XML the PATH ( ' ' )),
 . 1 ,
 . 1 ,
 ' '
)
--Stuff the first separator '***' taken 
the SELECT  STUFF is 
( 
( the SELECT  ' *** ' + UserName the FROM  @TAB  the FOR the XML the PATH ( '' )),
 . 1 ,
 . 3 ,
 '' 
)

 

Guess you like

Origin www.cnblogs.com/love-zf/p/11111859.html