【澳门金沙vip】SQL数据库批量插入数据

复制代码 代码如下: 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

数据插入: 

什么是存储过程

1、事实数据插入:通过DTS加调度实现将sales_fact_1997的数据进行复制。频率为每分钟10000条。主要用到的SQL语句:select
top 10000 * from sales_fact_1997 

存储过程是一组为了完成特定功能的sql语句集,是利用sql
server所提供的transact-sql语言所编写的程序。经编译后存储在数据库教程中。存储过程是数据库中的一个重要对象,用户通过指定存储过程的名字并给出参数来执行它。存储过程是由流控制和sql语句书写的过程,这个过程经编译和优化后存储在数据库服务器中,存储过程可由应用程序通过一个调用来执行,而且允许用户声明变量
。同时,存储过程可以接收和输出参数、返回执行存储过程的状态值

2、维度数据插入:通过SQL语句插入数据到time_by_day. 

存储过程语法

测试用到的SQL语句: 

create procedure [拥有者.]存储过程名[;程序编号]
[(参数#1,参数#1024)] [with {recompile | encryption | recompile,
encryption} ] [for replication]

1、单条插入 

看一个简单的实例

INSERT INTO time_by_day 

create procedure order_tot_amt @o_id int, @p_tot int output as
select @p_tot = sum(unitprice*quantity) from orderdetails where
ordered=@o_id go

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

下面来看一个利用存储过程批量导入数据实例

VALUES (‘1101’, ‘1999-10-1’, ‘1999’, ’10’, ‘Q4′,’1’)

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

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 /*设置变量*/