用SQL数据库批量插入数据

来源:互联网 发布:人工智能行业投资前景 编辑:程序博客网 时间:2024/05/08 12:18

测试用到的SQL语句:

1、单条插入

  1. INSERT INTO time_by_day 
  2. (time_id, the_date, the_year, month_of_year, quarter,day_of_month) 
  3. VALUES ('1101''1999-10-1''1999''10''Q4','1')

2、单条插入:

  1. INSERT INTO time_by_day 
  2. (time_id, the_date, the_year, month_of_year, quarter, day_of_month) 
  3. SELECT TOP 1 time_id + 1 AS time_id, the_date + 1 AS the_date, YEAR(the_date + 1) 
  4. AS the_year, MONTH(the_date + 1) AS month_of_year, { fn QUARTER(the_date + 1) 
  5. } AS quarter, DAY(the_date + 1) AS day_of_month 
  6. FROM time_by_day 
  7. ORDER BY time_id DESC

      3、循环插入:

  1. DECLARE @MyCounter INT 
  2. SET @MyCounter = 0            /*设置变量*/ 
  3. WHILE (@MyCounter < 2)     /*设置循环次数*/ 
  4. BEGIN 
  5. WAITFOR DELAY '000:00:10'   /*延迟时间10秒*/ 
  6. INSERT INTO time_by_day 
  7. (time_id, the_date, the_year, month_of_year, quarter, day_of_month) 
  8. SELECT TOP 1 time_id + 1 AS time_id, the_date + 1 AS the_date, YEAR(the_date + 1) 
  9. AS the_year, MONTH(the_date + 1) AS month_of_year, { fn QUARTER(the_date + 1) 
  10. } AS quarter, DAY(the_date + 1) AS day_of_month 
  11. FROM time_by_day 
  12. ORDER BY time_id DESC 
  13. SET @MyCounter = @MyCounter + 1 
  14. END
       4、插入以时间为变量的数据
  1. DECLARE @MyCounter INT 
  2. declare @the_date datetime 
  3. SET @MyCounter = 0 
  4. SET @the_date = '1999-1-4' 
  5. WHILE (@MyCounter < 200000) 
  6. BEGIN 
  7. WAITFOR DELAY '000:00:10' 
  8. /*INSERT INTO time_by_day 
  9. (time_id, the_date, the_year, month_of_year, quarter, day_of_month) 
  10. SELECT TOP 1 time_id + 1 AS time_id, the_date + 1 AS the_date, YEAR(the_date + 1) 
  11. AS the_year, MONTH(the_date + 1) AS month_of_year, { fn QUARTER(the_date + 1) 
  12. } AS quarter, DAY(the_date + 1) AS day_of_month 
  13. FROM time_by_day 
  14. ORDER BY time_id DESC 
  15. */ 
  16. insert into time_by_day (time_id,the_date)values('371',@the_date) 
  17. SET @the_date = @the_date + 1 
  18. SET @MyCounter = @MyCounter + 1 
  19. END  

在网上找的.留下来做个记录....

原创粉丝点击