Saturday, March 7, 2026

How to add multiple insert values with for loop in SQL Server

 

    DECLARE @i INT =  1; -- Declare a variable for the loop counter

    DECLARE @max_value INT = 500; -- Set the loop limit

    -- Start the WHILE loop

    WHILE @i <= @max_value 

begin

        -- 3. The INSERT query inside the loop

         SET @i = @i + 1;

INSERT INTO [dbo].[AgentMaster]

           ([AgentName])

     VALUES

           ('test')

        end


  /****** Script for SelectTopNRows command from SSMS  ******/

SELECT [AgentNo]

      ,[AgentName]

  FROM [testdb].[dbo].[AgentMaster]

No comments:

Post a Comment