SQL Server Loop Insert

Source: Internet
Author: User

A SQL loop inserts the code that works fine:

BEGIN DECLARE @idx as INT; DECLARE @NodeName nvarchar (255); DECLARE @OtherName nvarchar (255); DECLARE @ParentId INT; DECLARE @OrderId Int; DECLARE @Url nvarchar (255); DECLARE @NodeClass nvarchar (255); DECLARE @myTable TABLE (NodeName nvarchar (255), Othername nvarchar (255), ParentID int,orderid int,url nvarchar (255), Nodeclass nvarchar (255)) INSERT into @myTable select Nodename,othername,parentid,orderid,url,nodeclass from Sysmenu where othername = ' super Administrator '; declare @count int SELECT @count =count (*) from @myTable set @idx =1; While @idx <= @count BEGIN Select @NodeName =NodeName, @OtherName =othername, @ParentId =ParentID, @OrderId =OrderId, @Url =Url, @NodeClass =nodeclass from (select Row_number () over (order by OrderId) num,* from @myTabl e) A where [email protected]; INSERT into sysmenu (Nodename,othername,parentid,orderid,url,nodeclass) VALUES (@NodeName, ' Sales design channel ', @ParentId,- 1, @Url, @NodeClass); SET @idx = @idx + 1; End End              

General idea:

1. Create a temporary table mytable, insert the data that needs to be inserted into the loop.

2, declare a variable, record the total amount of data: declare @count int SELECT @count =count (*) from @myTable.

3, with Wihle loop insert.

Question: How do I fetch every piece of data in a temporary table mytable?

Solution: Refer to the code and use the RowNumber function to sort it out in turn.

SQL Server Loop Insert

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.