01.Blogs :
clintz  

T-SQL: SP to Import Excel file to MSSQL

create procedure SP_ImportExcelFile(
 @Source varchar(1000)
 , @SourceSheet varchar(100)
 , @DestinationTable varchar(100))
as

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

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

-- check if worksheet exists and if not use Sheet1
if @SourceSheet is null or @SourceSheet = ''
 set @SourceSheet = '[Sheet1$]'
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 + ')')

posted on Friday, December 02, 2005 7:17 PM by clintz


 
03.UPDATE CALENDAR :
<December 2005>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

05.MY LINKS :

07.Subscriptions :

Subscriptions


© Copyright 2005 Microsoft Corporation. All Rights Reserved.
Terms of Use | Privacy Statement | Code of Conduct | Hosted by MaximumASP for Microsoft
WHO-BAR