SQL loop table data, split Function

Source: Internet
Author: User

There are two small knowledge points. To make it easier for you to read the information, you also need to develop a habit. A blog writing habit. I read it last time.Article. It is about the benefits of blog writing.

I have read it, and I have some feelings. I also know that I may not be able to stick to it. But I want to try it. Try to develop such a habit.

1. Data in the SQL loop table

 Select   *   Into # T1 From  Userinfo  Declare   @ ID   Varchar ( 2000 );  While ( Exists ( Select Projectid From  # T1 ))  Begin       Select   @ ID  = Userid From  # T1  Delete # T1 Where Projectid = @ ID  End  Drop   Table # T1

2 SQL sharding Functions

 Use   [  Xsman_db  ]  Go  /*  * *** Object: userdefinedfunction [DBO]. [func_splittotable] script Date: 12/03/2012 23:25:37 *****  */  Set Ansi_nulls On Go  Set Quoted_identifier On  Go  --  ========================================================== =====  --  Author: cxy  --  Create Date: 2010-10-28  --  Description: splits the string according to the delimiter and returns the table  --  ========================================================== =====  Create  Function   [  DBO  ] . [  Func_splittotable  ]  (  @ Splitstring   Nvarchar ( Max  ),  @ Separator   Nvarchar ( 10 ) = '   '  )  Returns   @ Splitstringstable   Table  (  [  ID  ]   Int   Identity ( 1 , 1  ),  [ Value  ]   Nvarchar ( Max  ))  As    Begin     Declare   @ Currentindex   Int  ;  Declare   @ Nextindex   Int  ;  Declare   @ Returntext  Nvarchar ( Max  );  Select   @ Currentindex  =  1  ;  While ( @ Currentindex  <=  Len ( @ Splitstring  ))  Begin      Select  @ Nextindex  =  Charindex ( @ Separator , @ Splitstring , @ Currentindex  );  If ( @ Nextindex  =  0   Or   @ Nextindex   Is   Null  ) Select   @ Nextindex  =  Len ( @ Splitstring ) +  1  ;  Select   @ Returntext  =  Substring ( @ Splitstring , @ Currentindex , @ Nextindex -  @ Currentindex  );  Insert   Into   @ Splitstringstable ( [  Value  ] ) Values ( @ Returntext  );  Select   @ Currentindex  =  @ Nextindex +  1  ;  End     Return    End  --  Execute function 1  Select   *   From DBO. func_splittotable ( '  1, 2  ' , '  ,  ' )  --  Execute function 2  Select   *   From DBO. func_splittotable ( '  1-2-3  ' , '  -  '  )  --  Execute function 3  Select   *  From DBO. func_splittotable ( '  1 2 3  ' , '   ' )

 

Execution result:

 

I hope I can stick to it. Effort!

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.