将Excel工作表数据批量导入SQL Server数据库表的存储过程

时间:2014-05-11 17:54:04

标签: sql-server excel stored-procedures

alter procedure _sp_ImportExcelFile            
            (@Source varchar(1000)
            , @SourceSheet varchar (100)
            , @DestinationTable varchar (100))
as
   declare @retval int

   EXEC master..xp_fileexist @Source, @retval output -- check if file exists

   if @retval = 0
            begin
                        print 'file does not exist.'
                        return
            end

   if @SourceSheet is null or @SourceSheet = ''
            set @SourceSheet = '[projects_export$]' 
   else
            set @SourceSheet = '[' + ltrim(rtrim(@SourceSheet)) + '$]'

   if @DestinationTable is null or @DestinationTable = ''
            set @DestinationTable = substring(@SourceSheet, 2, len(@SourceSheet) - 3) + convert(varchar, getdate(), 126)

   exec('select * into [' + @DestinationTable + '] from openrowset
(''Microsoft.Jet.OLEDB.4.0'', ''Excel 8.0;HDR=YES;Database=' + @Source + ''', ' + 
@SourceSheet + ')')

我正在使用它作为执行语句

exec _sp_ImportExcelFile 'D:\exceldata\','projects_export','ExcelData'

其中projects_export是工作表的名称,ExcelData是我的工作表的名称 但得到的错误为file doesn't exist

0 个答案:

没有答案