编者注:你可曾想过要如何将Excel表中的数据导入到SQL Server中?在导入的时候,你是否能找到自己的主键呢?笔者通过一个例子告诉你,该怎么导入,希望对大家有所帮助。
有人提问如下:
这个是Excel的,比如是test.xls
/*******************建立测试数据***3w@live.cn***********************/
DROP TABLE [TBTest]
CREATE TABLE [TBTest](
[date] NVARCHAR(20) null,
--------go
--------sp_configure 'show advanced options', 1
------------reconfigure
--------sp_configure 'Ad Hoc Distributed Queries', 1
--------reconfigure
(PKId int primary key identity(1,1)
,EMonth int not null,RMoney Decimal(15,2) not null
)
insert into @TableVar
/*******************第一种方法,用游标***3w@live.cn***********************/
DECLARE @RYear int
declare @EMonth int
select RYear,BMonth,EMonth,RMoney from @TableVar where 1=1
FETCH NEXT FROM DateDemo_cursor
BEGIN
----print @BMonth
----print @RMoney
--修改记录
insert INTO [TBTest]
CASE WHEN (@BMonth<10) THEN '0'+cast(@BMonth AS nvarchar(2))
end
FETCH NEXT FROM DateDemo_cursor into @RYear,@BMonth,@EMonth,@RMoney
END
GO
tid date Money
/*
******************第二种方法,用CTE,适用于sql2005/2008/2008 r2*********/
TRUNCATE table [TBTest]
(PKId int primary key identity(1,1)
,EMonth int not null,RMoney Decimal(15,2) not null
insert into @TableVar(RYear ,BMonth ,EMonth ,RMoney)
with seq as (select top 12 row_number() over (order by object_id) val
CASE WHEN (t.BMonth+seq.val<10) THEN '0'+cast(t.BMonth+seq.val AS nvarchar(2))
,RMoney c
number between 1 and 5
number
/***************************************3w@live.cn***********************/
Declare @TableVar table
,EMonth int not null,RMoney Decimal(15,2) not null
);
insert into @TableVar
select
from
number between BMonth and EMonth
type='p'
思路六:使用SSIS实现
原文链接:http://www.cnblogs.com/downmoon/archive/2011/05/02/2034191.html