Code for reading the number and name of sheet in Excel using SQL scripts

Source: Internet
Author: User

Copy codeThe Code is as follows:
-- Get table (worksheet) or column (field) listings from an excel spreadsheet

-- Set variables
Declare @ linkedServerName sysname = 'tempexcelspreadsheet'
Declare @ excelFileUrl nvarchar (1000) = 'd: \ text.xlsx'
--/SET

-- Delete the Link Service (if it already exists)
If exists (select null from sys. servers where name = @ linkedServerName) begin
Exec sp_dropserver @ server = @ linkedServerName, @ droplogins = 'droplogins'
End

-- Add a service object
-- ACE 12.0 works well for *. xls and *. xlsx. You can also use Jet, but you can only access *. xls files.
Exec sp_addmediaserver
@ Server = @ linkedServerName,
@ Srvproduct = 'ace 12.0 ',
@ Provider = 'Microsoft. ACE. OLEDB.12.0 ',
@ Datasrc = @ excelFileUrl,
@ Provstr = 'excel 12.0; HDR = yes'

-- Get current user
Declare @ suser_sname nvarchar (256) = suser_sname ()

-- Add the current user as the login Link Service
Exec sp_add1_srvlogin
@ Rmtsrvname = @ brief servername,
@ Useself = 'false ',
@ Locallogin = @ suser_sname,
@ Rmtuser = null,
@ Rmtpassword = null

-- Returns the columns in the sheet and each sheet.
Exec sp_tables_ex @ linkedServerName
Exec sp_columns_ex @ linkedServerName

-- Delete a linked service object

If exists (select null from sys. servers where name = @ linkedServerName) begin
Exec sp_dropserver @ server = @ linkedServerName, @ droplogins = 'droplogins'
End

Related Article

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.