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