Bulk Import all Excel files under the Development folder into Microsoft SQL database

Source: Internet
Author: User

The following code imports all excle data from the C:\cs\ folder into the SQL database

Declare @query VARCHAR (1000)

DECLARE @max1 int

DECLARE @count1 int

DECLARE @filename varchar (100)

Set @count1 =0

CREATE TABLE #x (name varchar (200))

Insert #x exec (' Master.dbo.xp_cmdshell ' dir c:\cs\*.xls/b ')

Delete from #x where name is null

Select IDENTITY (int,1,1) as ID, name into #y from #x

drop table #x

Set @max1 = (select MAX (ID) from #y)


While @count1 <= @max1

Begin

Set @[email protected]+1

Set @filename = (select name from #y where [email protected])


Set @query = ' SELECT * to dbo.tab9 from OPENROWSET (' MICROSOFT. JET. oledb.4.0 ', ' Excel 5.0; Hdr=yes;database=c:\cs\ ' [email protected]+ ', ' select * from [January $b11:e27] '

Print @query

EXEC @query

End

drop table #y

Go

To Dbo.tab9 from OPENROWSET (' MICROSOFT. JET. oledb.4.0 ', ' Excel 5.0; Hdr=yes;database=c:\cs\ Shanghai. xls ', ' select * from [January $b11:e27] ')

SELECT * into DBO.TAB9 from OPENROWSET (' MICROSOFT. JET. oledb.4.0 ', ' Excel 5.0; Hdr=yes;database=c:\cs\ Shanghai. xls ', ' select * from [January $b11:e27] ')

EXEC (@query)


This article is from the "Han Li" blog, please be sure to keep this source http://91xueit.blog.51cto.com/400469/1982943

Bulk Import all Excel files under the Development folder into Microsoft SQL database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.