1. 程式人生 > >用SQL儲存過程實現批量插入資料

用SQL儲存過程實現批量插入資料

1,單條插入

INSERT INTO time_by_day

 

(time_id, the_date, the_year, month_of_year, quarter,day_of_month)

VALUES ('1101', '1999-10-1', '1999', '10', 'Q4','1')

2,單條插入:

INSERT INTO time_by_day

      (time_id, the_date, the_year, month_of_year, quarter, day_of_month)

SELECT TOP 1 time_id + 1 AS time_id, the_date + 1 AS the_date, YEAR(the_date + 1)

      AS the_year, MONTH(the_date + 1) AS month_of_year, { fn QUARTER(the_date + 1)

      } AS quarter, DAY(the_date + 1) AS day_of_month

FROM time_by_day

ORDER BY time_id DESC

3,迴圈插入:

DECLARE @MyCounter INT

SET @MyCounter = 0            /*設定變數*/

WHILE (@MyCounter < 2)     /*設定迴圈次數*/

BEGIN

WAITFOR DELAY '000:00:10'   /*延遲時間10秒*/

INSERT INTO time_by_day

      (time_id, the_date, the_year, month_of_year, quarter, day_of_month)

SELECT TOP 1 time_id + 1 AS time_id, the_date + 1 AS the_date, YEAR(the_date + 1)

      AS the_year, MONTH(the_date + 1) AS month_of_year, { fn QUARTER(the_date + 1)

      } AS quarter, DAY(the_date + 1) AS day_of_month

FROM time_by_day

ORDER BY time_id DESC

SET @MyCounter = @MyCounter + 1

END

4,插入以時間為變數的資料

DECLARE @MyCounter INT

declare @the_date datetime

SET @MyCounter = 0

SET @the_date = '1999-1-4'

WHILE (@MyCounter < 200000)

BEGIN

WAITFOR DELAY '000:00:10'

/*INSERT INTO time_by_day

      (time_id, the_date, the_year, month_of_year, quarter, day_of_month)

SELECT TOP 1 time_id + 1 AS time_id, the_date + 1 AS the_date, YEAR(the_date + 1)

      AS the_year, MONTH(the_date + 1) AS month_of_year, { fn QUARTER(the_date + 1)

      } AS quarter, DAY(the_date + 1) AS day_of_month

FROM time_by_day

ORDER BY time_id DESC

*/

insert into time_by_day (time_id,the_date)values('371',@the_date)

SET @the_date = @the_date + 1

SET @MyCounter = @MyCounter + 1

END