sql 语句 循环插入100万条数据到某个表中

时间:2021-06-17 19:19:46


use M3_2011

declare @i bigint =1
--declare @j int=1
declare @k smallint =1


 begin Transaction
  begin Try
  
  ----------------------------------
    while(@i<= 1000000)
    begin

     INSERT INTO [M3_2011].[dbo].[fa_Client_Alter]
          ([Alter_ID]
          ,[Client_ID]
          ,[Alter_Date]
          ,[Cost]
          ,[Alter_Kind]
          ,[Status]
          ,[Create_User]
          ,[Create_Date]
          ,[Audit_User]
          ,[Audit_Date]
          ,[Remark])
       select
          @i
          ,@i
          ,'20110215'
          ,@i*1.1
          ,'Alter_Kind' + CONVERT(varchar,@i)
          ,@k
          ,'Create_User' + CONVERT(varchar,@i)
          ,'20110215'
          ,'Audit_User' + CONVERT(varchar,@i)
          ,'20110215'
          ,'Remark' + CONVERT(varchar,@i)
     
      set @i=@i + 1
 
      --set @j=@j +1
      
      if(@k=32767)
       set @k=@k -32767 
       
      set @k=@k + 1 
    end  
  ---------------------------------
   commit Tran
   
  end Try
  begin Catch
  
   rollback Tran
   
   declare @sTemp varchar(1000)
   select @sTemp=ERROR_MESSAGE()
   raiserror(@sTemp,16,1)
   
  end Catch