sql 導入導出操作Excel

USE BW_VoiceDispatch

--1、數據庫導出到Excel
insert into OpenRowSet('Microsoft.ACE.OLEDB.12.0','Excel 12.0;hdr=yes;database=D:\1.xls;','select * from [Sheet1$]')--(id,value)
--select * from t1
select id,value from t1


--2、查詢Excel
--2.1 使用OpenRowSet
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;HDR=YES;DATABASE=d:\1.xls',sheet1$)
select * from OpenRowSet('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;HDR=Yes;IMEX=1;Database=D:\1.xls', 'select * from [sheet1$]')  
select * from OpenRowSet('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;HDR=Yes;IMEX=1;Database=D:\1.xls', 'select * from [sheet1$]') where ID=1  
select * from OpenRowSet('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;HDR=Yes;IMEX=1;Database=D:\1.xls', 'select value,id from [sheet1$]')


--2.2 使用opendatasource
select * from opendatasource('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=d:\1.xls')...[t1$]


--3、修改Excel
update  OpenRowSet('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;HDR=Yes;IMEX=0;Database=D:\1.xls', 'select * from [t1$]')
set c1=1 where id=1 

--4、導入Excel數據,同時創建表:
select * into t1 FROM openrowset( 'Microsoft.ACE.OLEDB.12.0','EXCEL 12.0;HDR=YES;IMEX=1;Database=D:\1.xls','select * from [t1$]');
SELECT * INTO t2 FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;HDR=YES;DATABASE=d:\1.xls',sheet1$)


--5、導入excel到數據庫已有表
insert into t1
select * from OpenRowSet('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;HDR=Yes;IMEX=1;Database=D:\1.xls', 'select * from [t1$]')  

 

--6、導入excel到數據庫已有表,t1表中ID爲自動標示時
delete t1
SET IDENTITY_INSERT t1 ON
insert into t1(id,value)
select id,value from OpenRowSet('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;HDR=Yes;IMEX=1;Database=D:\1.xls', 'select id,value from [t1$]')  
SET IDENTITY_INSERT t1 off

declare @MaxCount int
set @MaxCount=(select max(id) from t1)
--修改標示種子            
DBCC  CHECKIDENT  (t1,RESEED,@MaxCount)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章