SQL Server Loop table

Source: Internet
Author: User
Tags rowcount

CREATE TABLEDbo.[User](UIDBIGINT IDENTITY, NameVARCHAR( -) not NULL, PWDVARCHAR( -) not NULL ,CONSTRAINTPk_userPRIMARY KEY CLUSTERED(UID)) on [PRIMARY]
--Method 2: Use Table variables--declaring table VariablesDECLARE @temp TABLE(UIDBIGINT, NameVARCHAR( -), PwdVARCHAR( -));--inserting data from a source table into a table variableINSERT  into @temp(UID, Name, PWD)SELECTUid,name,pwd from [User]ORDER  byUID;--declaring VariablesDECLARE    @UID  as BIGINT,    @Name  as VARCHAR( -),    @Pwd  as VARCHAR( -);  while EXISTS(SELECTUid from @temp)BEGIN    --unordered heap Top1 has index TOP1 SET ROWCOUNT 1 No difference    SELECT Top 1 @UID=Uid@Name=Name,@Pwd=Pwd from @temp; --SELECT @UID, @Name, @Pwd    PRINT 'PRIMARY Key'+ cast(@UID  as varchar( -))+'User name'+@Name+'Password'+@Pwd   --SET ROWCOUNT 0    DELETE  from @temp WHEREUid=@UID;END

SQL Server Loop table

Related Article

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.