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)

 

你可能感兴趣的:(sql 导入导出操作Excel)