title: How to Reduc the while loop with code sqlserver
body: <p>Master two hundred strokes a day , each corresponding to one hundred strokes than master sub- files, a total of one year is 7.3 million pen information
  Here is my SQL command</p>
  
  <pre><code>DECLARE @day INT =0
  DECLARE @everyday DATETIME
  DECLARE @mastercount INT=0
  DECLARE @detailcount INT=0
  DECLARE @state NVARCHAR(3)='ship'
  DECLARE @user VARCHAR(8)='Sid'
  DECLARE @masterid INT
  
  SET nocount ON
  
  WHILE( @day &lt;= 364 )
    BEGIN
        IF( @day = 122 )
          BEGIN
              SET @user='danny'
              SET @state='stock'
          END
        ELSE IF( @day = 244 )
          BEGIN
              SET @user='annie'
              SET @state='ship'
          END
  
        SET @everyday=Dateadd(yy, Datediff(yy, 0, Getdate()), @day)
  
        WHILE( @mastercount &lt; 200 )
          BEGIN
              INSERT INTO OrderMaster
                          (OrderCustomerName,orderdate,orderstate)
              VALUES      (@user,@everyday,@state)
  
              SELECT @masterid = C_Id
              FROM   OrderMaster
              WHERE  C_Id = Scope_identity()
  
              WHILE( @detailcount &lt; 100 )
                BEGIN
                    INSERT INTO OrderDetail
                                (C_OrderMasterId,C_ProductMasterId,Quantity)
                    VALUES      (@masterid,1 + ( 21 - 1 ) * Rand(),Rand() * 100 + 1)
  
                    SET @detailcount=@detailcount + 1
                END
  
              SET @detailcount=0--
              SET @mastercount=@mastercount + 1
          END
  
        SET @mastercount=0
        SET @day=@day + 1
    END
  
  SET nocount OFF 
  </code></pre>
  
  <p>However
  The total time taken 51min
  Too long ~~
  How can I shorten the time?
  Thank you ~!</p>