SQL Server - IMPORT EXCEL SHEET TO DATABASE USING STORED PROCEDURE

Asked By jayaram thoram
01-Aug-11 08:09 AM
Hii,

   I want know how to import excel sheet data into databse using stored procedure in sql server
  Vickey F replied to jayaram thoram
01-Aug-11 08:12 AM
Use this -

INSERT Personal (Name, ID) SELECT Name, ID
FROM OPENROWSET('MSDASQL',
'Driver={Microsoft Excel-Treiber (*.xls)};
DBQ=<filename>;HDR=NO',
'SELECT * FROM [Tabelle1$]')


Exchange <filename> with the filename
if the excel-sheet contains header lines set HDR=YES,
[Tabelle1$] is the name of the excel-sheet.


Follow this link-
http://www.sqlservercentral.com/Forums/Topic664839-9-1.aspx#bm1093736
  TSN ... replied to jayaram thoram
01-Aug-11 08:15 AM
hi..

Follow this link ..

http://www.aspsnippets.com/Articles/Read-and-Import-Excel-Sheet-into-SQL-Server-Database-in-ASP.Net.aspx
  Ravi S replied to jayaram thoram
01-Aug-11 08:16 AM
HI

try htis

Stored Procedures

For this article I have created two stored procedures one to read the Excel 97 – 2003 format and other Excel 2007 format. Though the Microsoft Ace Driver can read both still I have used Jet for Excel 97 - 2003 formats.

              

 

 

 

Excel 97 – 2003 Format

 

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE spx_ImportFromExcel03

    @SheetName varchar(20),

    @FilePath varchar(100),

    @HDR varchar(3),

    @TableName varchar(50)

AS

BEGIN

    DECLARE @SQL nvarchar(1000)

       

    IF OBJECT_ID (@TableName,'U') IS NOT NULL

    SET @SQL = 'INSERT INTO ' + @TableName + ' SELECT * FROM OPENDATASOURCE'

    ELSE

    SET @SQL = 'SELECT * INTO ' + @TableName + ' FROM OPENDATASOURCE'

 

    SET @SQL = @SQL + '(''Microsoft.Jet.OLEDB.4.0'',''Data Source='

    SET @SQL = @SQL + @FilePath + ';Extended Properties=''''Excel 8.0;HDR='

    SET @SQL = @SQL + @HDR + ''''''')...['

    SET @SQL = @SQL + @SheetName + ']'

    EXEC sp_executesql @SQL

END

GO

 

Excel 2007 Format

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE spx_ImportFromExcel07

   @SheetName varchar(20),

   @FilePath varchar(100),

   @HDR varchar(3),

   @TableName varchar(50)

AS

BEGIN

    DECLARE @SQL nvarchar(1000)

   

    IF OBJECT_ID (@TableName,'U') IS NOT NULL

    SET @SQL = 'INSERT INTO ' + @TableName + ' SELECT * FROM OPENDATASOURCE'

    ELSE

    SET @SQL = 'SELECT * INTO ' + @TableName + ' FROM OPENDATASOURCE'

 

    SET @SQL = @SQL + '(''Microsoft.ACE.OLEDB.12.0'',''Data Source='

    SET @SQL = @SQL + @FilePath + ';Extended Properties=''''Excel 12.0;HDR='

    SET @SQL = @SQL + @HDR + ''''''')...['

    SET @SQL = @SQL + @SheetName + ']'

    EXEC sp_executesql @SQL

END

GO

 

In the above stored procedures, I have used four input parameters

1.@SheetName - Name of the Excel Sheet to be read.

2.@FilePath - Path of the Excel File

3.@HDR - Indicates whether first row in the excel sheet will be considered as Header row or not.

4.@TableName - The name of the table in which the Excel Sheet data will be transferred if the table is not present it will be created.

 

When you run the above stored procedure first time you might get the following error message.

Msg 15281, Level 16, State 1, Line 1

SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.


In order to run the above stored procedure you will need to enable Surface Area Configuration in your SQL Server in the following way

Execute the below four statements one by one in the SQL Server Query Analyzer

 

sp_configure 'show advanced options', 1

 


reconfigure

 

 

sp_configure 'Ad Hoc Distributed Queries', 1

 

 

reconfigure

 

 

Also in order to use the Microsoft OLEDB Ace Driver you will need to install the 2007 Office System Driver: Data Connectivity Components which is necessary for Microsoft OLEDB ACE 12.0 driver to work using the link below

http://www.microsoft.com/downloads/details.aspx?familyid=7554F536-8C28-4598-9B72-EF94E038C891

I faced the following Error while running the OLEDB Ace stored procedure

Msg 7399, Level 16, State 1, Line 2

The OLE DB provider "Microsoft.Ace.OLEDB.12.0" for linked server "(null)" reported an error. Access denied.

Msg 7350, Level 16, State 2, Line 2

Cannot get the column information from OLE DB provider "Microsoft.Ace.OLEDB.12.0" for linked server "(null)".

 

I have written an article on the solutions for it. You can visit it using the link below

http://www.aspsnippets.com/post/2009/06/06/The-OLE-DB-provider-MicrosoftAceOLEDB120-for-linked-server-%28null%29.aspx


Front End Design

Below is the markup of the HTML mark of the asp.net web page. There are 2 panels. First with an upload button, Asp.Net FileUpload control and a label to display the status used to upload the Excel File. Second one with label to display the uploaded Excel file name, DropDownList which contain the names of Sheets of the Excel workbook, an ASP.Net RadioButtonList to capture whether the Sheet has header row and finally two buttons one to import the Excel Sheet rows into the SQL Server Database table and other one to cancel.

 

<asp:Panel ID="Panel1" runat="server">

    <asp:FileUpload ID="FileUpload1" runat="server" />

    <asp:Button ID="btnUpload" runat="server" Text="Upload"

       OnClick="btnUpload_Click" />

    <br />

    <asp:Label ID="lblMessage" runat="server" Text="" />

</asp:Panel>

<asp:Panel ID="Panel2" runat="server" Visible = "false" >

    <asp:Label ID="Label5" runat="server" Text="File Name"/>

    <asp:Label ID="lblFileName" runat="server" Text=""/>

    <br />

    <asp:Label ID="Label2" runat="server" Text="Select Sheet" />

    <asp:DropDownList ID="ddlSheets" runat="server"

            AppendDataBoundItems = "true">

    </asp:DropDownList>

    <br />

    <asp:Label ID="Label3" runat="server" Text="Enter Source Table Name"/>

    <asp:TextBox ID="txtTable" runat="server"></asp:TextBox>

    <br />

    <asp:Label ID="Label1" runat="server" Text="Has Header Row?" />

    <br />

    <asp:RadioButtonList ID="rbHDR" runat="server">

      <asp:ListItem Text = "Yes" Value = "Yes" Selected = "True" >

      </asp:ListItem>

      <asp:ListItem Text = "No" Value = "No"></asp:ListItem>

    </asp:RadioButtonList>

    <br />

    <asp:Button ID="btnSave" runat="server" Text="Save"

      OnClick="btnSave_Click" />

    <asp:Button ID="btnCancel" runat="server" Text="Cancel"

      OnClick="btnCancel_Click" />     

 </asp:Panel>




Namespaces

You will require to import the following namespaces

C#

using System.Data;

using System.Data.OleDb;

using System.IO;

using System.Configuration;

using System.Data.SqlClient;

 

VB.Net

Imports System.Data

Imports System.Data.OleDb

Imports System.IO

Imports System.Data.SqlClient

Imports System.Configuration

 

 

Web.Config Configurations

I have used a AppSettings key FolderPath to store the path of the folder where the uploaded excel file will be stored.

There are three connections strings as described below

1. Excel03ConString - Conncection String for Excel 97 – 2003 formats

2. Excel07ConString - Connection String for Excel 2007 format

3. conString - Connection String for the SQL Server 2005 Express Database.


<appSettings>

    <add key="FolderPath" value="Files/"/>

</appSettings>

 

<connectionStrings>

<add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;

        Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"/>

<add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;

        Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"/>

<add name="conString" connectionString="Data Source=.\SQLEXPRESS;

        database=ExcelImport;Integrated Security=true"/>

</connectionStrings>

 


Uploading the excel Workbook

User has to first upload the Excel File whose data he wants to transfer to the SQL Server database  onto the server using ASP.Net FileUpload Control and a Upload button. The code snippet for the Upload Button is given below.

 

C#

protected void btnUpload_Click(object sender, EventArgs e)

{

    if (FileUpload1.HasFile)

    {

      string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

      string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);

      string FolderPath = ConfigurationManager.AppSettings["FolderPath"];

      string FilePath = Server.MapPath(FolderPath + FileName);

      FileUpload1.SaveAs(FilePath);

      GetExcelSheets(FilePath, Extension, "Yes");

    }

}

 

 VB.Net

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)

  If FileUpload1.HasFile Then

    Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)

    Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)

    Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")

    Dim FilePath As String = Server.MapPath(FolderPath + FileName)

    FileUpload1.SaveAs(FilePath)

    GetExcelSheets(FilePath, Extension, "Yes")

  End If

End Sub

 

The above code snippet simply uploads the Excel Workbook into the path defined in the Web.Config key. The figure below displays the User Interface for uploading the Excel File.


User Inteface : Uploading the Excel WorkBook

You will notice GetExcelSheets function being called on the click of Upload Button. As the name suggests the function reads the names of all the sheets present in the Excel Workbook and binds the result to DropDownList. The complete function is given below

        

C#

private void GetExcelSheets(string FilePath, string Extension, string isHDR)

{

    string conStr="";

    switch (Extension)

    {

      case ".xls": //Excel 97-03

        conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"]

           .ConnectionString;

        break;

      case ".xlsx": //Excel 07

        conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"]

           .ConnectionString;

        break;

    }

 

    //Get the Sheets in Excel WorkBoo

    conStr = String.Format(conStr, FilePath, isHDR);

    OleDbConnection connExcel = new OleDbConnection(conStr);

    OleDbCommand cmdExcel = new OleDbCommand();

    OleDbDataAdapter oda = new OleDbDataAdapter();

    cmdExcel.Connection = connExcel;

    connExcel.Open();

 

    //Bind the Sheets to DropDownList

    ddlSheets.Items.Clear(); 

    ddlSheets.Items.Add(new ListItem("--Select Sheet--", ""));  

    ddlSheets.DataSource=connExcel

       .GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

    ddlSheets.DataTextField = "TABLE_NAME";

    ddlSheets.DataValueField = "TABLE_NAME";

    ddlSheets.DataBind();

    connExcel.Close();

    txtTable.Text = "";

    lblFileName.Text = Path.GetFileName(FilePath);

    Panel2.Visible = true;

    Panel1.Visible = false;

}

 

   

VB.Net

Private Sub GetExcelSheets(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String)

  Dim conStr As String = ""

  Select Case Extension

    Case ".xls"

     'Excel 97-03

     conStr = ConfigurationManager.ConnectionStrings("Excel03ConString") _

          .ConnectionString

     Exit Select

    Case ".xlsx"

   'Excel 07

   conStr = ConfigurationManager.ConnectionStrings("Excel07ConString") _

         .ConnectionString

     Exit Select

  End Select

 

  'Get the Sheets in Excel WorkBoo

  conStr = String.Format(conStr, FilePath, isHDR)

  Dim connExcel As New OleDbConnection(conStr)

  Dim cmdExcel As New OleDbCommand()

  Dim oda As New OleDbDataAdapter()

  cmdExcel.Connection = connExcel

  connExcel.Open()

 

  'Bind the Sheets to DropDownList

  ddlSheets.Items.Clear()

  ddlSheets.Items.Add(New ListItem("--Select Sheet--", ""))

  ddlSheets.DataSource = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid _

               .Tables, Nothing)

  ddlSheets.DataTextField = "TABLE_NAME"

  ddlSheets.DataValueField = "TABLE_NAME"

  ddlSheets.DataBind()

  connExcel.Close()

  txtTable.Text = ""

  lblFileName.Text = Path.GetFileName(FilePath)

  Panel2.Visible = True

  Panel1.Visible = False

End Sub




Importing the rows from the excel sheet to the database table

Once the Sheets are filed in the DropDownList the user interface looks as like below.


User Inteface : Selection of Excel Sheets and the destination database table

As you can see the excel file name is displayed along with all the sheets in the DropDownList. The RadioButtonList captures the information about the header row in the Excel Sheet. Then once the user presses Save button all the rows are read into the database table which the user has entered in the textbox if the table is not present it will be created.

The code snippet for the Save button is give below

   

C#

protected void btnSave_Click(object sender, EventArgs e)

{

    string FileName = lblFileName.Text;

    string Extension = Path.GetExtension(FileName);

    string FolderPath = Server.MapPath (ConfigurationManager

             .AppSettings["FolderPath"]);

    string CommandText = "";

    switch (Extension)

    {

      case ".xls": //Excel 97-03

        CommandText = "spx_ImportFromExcel03";

        break;

      case ".xlsx": //Excel 07

        CommandText = "spx_ImportFromExcel07";

        break;

    }

    //Read Excel Sheet using Stored Procedure

    //And import the data into Database Table

    String strConnString = ConfigurationManager

         .ConnectionStrings["conString"].ConnectionString;

    SqlConnection con = new SqlConnection(strConnString);

    SqlCommand cmd = new SqlCommand();

    cmd.CommandType = CommandType.StoredProcedure;

    cmd.CommandText = CommandText;

    cmd.Parameters.Add("@SheetName", SqlDbType.VarChar).Value =

           ddlSheets.SelectedItem.Text;

    cmd.Parameters.Add("@FilePath", SqlDbType.VarChar).Value =

           FolderPath + FileName;

    cmd.Parameters.Add("@HDR", SqlDbType.VarChar).Value =

           rbHDR.SelectedItem.Text;

    cmd.Parameters.Add("@TableName", SqlDbType.VarChar).Value =

           txtTable.Text;  

    cmd.Connection = con;

    try

    {

      con.Open();

      object count = cmd.ExecuteNonQuery();

      lblMessage.ForeColor = System.Drawing.Color.Green;

      lblMessage.Text = count.ToString() + " records inserted."

    }

    catch (Exception ex)

    {

      lblMessage.ForeColor = System.Drawing.Color.Red;  

      lblMessage.Text = ex.Message; 

    }

    finally

    {

      con.Close();

      con.Dispose();

      Panel1.Visible = true;

      Panel2.Visible = false;

    }

}

 

VB.Net

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)

  Dim FileName As String = lblFileName.Text

  Dim Extension As String = Path.GetExtension(FileName)

  Dim FolderPath As String = Server.MapPath( _

       ConfigurationManager.AppSettings("FolderPath"))

  Dim CommandText As String = ""

  Select Case Extension

    Case ".xls"

     'Excel 97-03

     CommandText = "spx_ImportFromExcel03"

     Exit Select

    Case ".xlsx"

     'Excel 07

     CommandText = "spx_ImportFromExcel07"

    Exit Select

  End Select

 

  'Read Excel Sheet using Stored Procedure

  'And import the data into Database Table

  Dim strConnString As String = ConfigurationManager _

     .ConnectionStrings("conString").ConnectionString

  Dim con As New SqlConnection(strConnString)

  Dim cmd As New SqlCommand()

  cmd.CommandType = CommandType.StoredProcedure

      cmd.CommandText = CommandText

  cmd.Parameters.Add("@SheetName", SqlDbType.VarChar).Value = _

      ddlSheets.SelectedItem.Text

  cmd.Parameters.Add("@FilePath", SqlDbType.VarChar).Value = _

      FolderPath + FileName

  cmd.Parameters.Add("@HDR", SqlDbType.VarChar).Value = _

      rbHDR.SelectedItem.Text

  cmd.Parameters.Add("@TableName", SqlDbType.VarChar).Value = _

      txtTable.Text

  cmd.Connection = con

  Try

    con.Open()

    Dim count As Object = cmd.ExecuteNonQuery()

    lblMessage.ForeColor = System.Drawing.Color.Green

    lblMessage.Text = count.ToString() & " records inserted."

  Catch ex As Exception

    lblMessage.ForeColor = System.Drawing.Color.Red

    lblMessage.Text = ex.Message

  Finally

    con.Close()

    con.Dispose()

    Panel1.Visible = True

    Panel2.Visible = False

  End Try

End Sub

 

The above code snippet simply calls the respective stored procedure based on the extension and the status is displayed to the user as shown in figure below


User Interface : Status notified to the user after completion of Excel Sheet import
  James H replied to jayaram thoram
01-Aug-11 08:16 AM
INSERT Personal (Name, ID) SELECT Name, ID
FROM OPENROWSET('MSDASQL',
'Driver={Microsoft Excel-Treiber (*.xls)};
DBQ=<filename>;HDR=NO',
'SELECT * FROM [Tabelle1$]')

Exchange <filename> with the filename
if the excel-sheet contains header lines set HDR=YES,
[Tabelle1$] is the name of the excel-sheet.
  Ravi S replied to jayaram thoram
01-Aug-11 08:18 AM
HI

use this code

protected void Button1_Click(object sender, EventArgs e)
   
{
       
       
//string path = Server.MapPath(".") + "\\Uploads\\" + FileUpload1.FileName;
       
//string path = Server.MapPath("Uploads/") +FileUpload1.FileName;
       
string name = FileUpload1.FileName;

        filepath
= Server.MapPath("Uploads/") + name;
       
FileUpload1.PostedFile.SaveAs(filepath);
        excelConnectionString
= "provider=Microsoft.jet.oledb.4.0;data source=" + filepath + ";extended properties='Excel 8.0;HDR=YES;'";

        writetodb
(filepath);
   
}

   
protected string valid(OleDbDataReader myreader, int stval)
   
{
       
//if any columns are found null then they are replaced by zero
       
object val = myreader[stval];
       
if (object.ReferenceEquals(val, DBNull.Value))
       
{
           
return Convert.ToString(0);
       
}
       
else
       
{
           
return val.ToString();
       
}
   
}
   
public void insertdataintosql(string fname, string lname, string city, string state)
   
{

       
SqlConnection conn = new SqlConnection(connString);
       
string query = "insert into details(fname,lname,city,state) values('" + fname + "','" + lname + "','" + city + "','" + state + "')";
       
SqlCommand cmd = new SqlCommand(query, conn);

        conn
.Open();
        cmd
.ExecuteNonQuery();
        conn
.Close();
   
}


   
private bool writetodb(string xlspath)
   
{

       
// Try
       
//OleDbConnection ocn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + xlspath + ";Extended Properties='Excel 8.0;HDR=Yes;'");
       
OleDbConnection excelConnection =new OleDbConnection(excelConnectionString);
        excelConnection
.Open();

       
try
       
{
           
OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", excelConnection);
            excelConnection
.Open();
           
OleDbDataReader odr = ocmd.ExecuteReader();
           
string fname = "";
           
string lname = "";
           
string city = "";
           
string state = "";
         
           
while (odr.Read())
           
{
                fname
= valid(odr, 0);
                lname
= valid(odr, 1);
                city
= valid(odr, 2);
                state
= valid(odr, 3);
               
                insertdataintosql
(fname,lname,city,state);
           
}
            excelConnection
.Close();
           
return true;
       
}
       
catch (DataException ee)
       
{
           
return false;
       
}
       
finally
       
{

            lblmsg
.Text = "Data Inserted Sucessfully";
            lblmsg
.ForeColor = System.Drawing.Color.Green;
       
}
   
}

refer
http://forums.asp.net/t/1536177.aspx
  TSN ... replied to jayaram thoram
01-Aug-11 08:18 AM
hi..

Refer:

http://www.sql-server-helper.com/tips/read-import-excel-file-p01.aspx

http://www.simple-talk.com/community/forums/thread/1189.aspx

http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/

  Riley K replied to jayaram thoram
01-Aug-11 08:18 AM

You may try this SQL fonction OPENROWSET, suppose you have an Excel file in the C:\ drive of the SQL Server database, and the data is on a Worksheet called Sheet1 and your two columns are called Column1 and Column2.

INSERT INTO MyTable
SELECT Column1, Column2
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\MyTable.xls',
'SELECT * FROM [Sheet1$]')
 
The first row of your Excel file should coutains the colums names.
Regards
  James H replied to jayaram thoram
01-Aug-11 08:20 AM
Refer: http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/4664c5e2-26a7-48f0-99f8-2e735cc37253

http://www.codescene.com/2006/06/importing-excel-into-sql-using.php

http://www.eggheadcafe.com/community/aspnet/7/10005108/importing-excel-data-to-s.aspx

  Reena Jain replied to jayaram thoram
01-Aug-11 08:39 AM
hi,

use bulkcopy option to copy bulk data from excel to sql server. Here is the code for you

// Connection String to Excel Workbook
string excelConnectionString = @"Provider=Microsoft
.Jet.OLEDB.4.0;Data Source=Book1.xls;Extended
Properties=""Excel 8.0;HDR=YES;""";
 
// Create Connection to Excel Workbook
using (OleDbConnection connection =
new OleDbConnection(excelConnectionString))
{
OleDbCommand command = new OleDbCommand
("Select ID,Data FROM [Data$]", connection);
 
connection.Open();
 
// Create DbDataReader to Data Worksheet
using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=.;
Initial Catalog=Test;Integrated Security=True";
 
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "ExcelData";
bulkCopy.WriteToServer(dr);
}
}
}

or use this stored procedure

\Hope this will help you
  Radhika roy replied to jayaram thoram
01-Aug-11 02:04 PM

Follow these solutions-

Excel 97 – 2003 Format

 

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE spx_ImportFromExcel03

    @SheetName varchar(20),

    @FilePath varchar(100),

    @HDR varchar(3),

    @TableName varchar(50)

AS

BEGIN

    DECLARE @SQL nvarchar(1000)

      

    IF OBJECT_ID (@TableName,'U') IS NOT NULL

    SET @SQL = 'INSERT INTO ' + @TableName + ' SELECT * FROM OPENDATASOURCE'

    ELSE

    SET @SQL = 'SELECT * INTO ' + @TableName + ' FROM OPENDATASOURCE'

 

    SET @SQL = @SQL + '(''Microsoft.Jet.OLEDB.4.0'',''Data Source='

    SET @SQL = @SQL + @FilePath + ';Extended Properties=''''Excel 8.0;HDR='

    SET @SQL = @SQL + @HDR + ''''''')...['

    SET @SQL = @SQL + @SheetName + ']'

    EXEC sp_executesql @SQL

END

GO

 

Excel 2007 Format

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE spx_ImportFromExcel07

   @SheetName varchar(20),

   @FilePath varchar(100),

   @HDR varchar(3),

   @TableName varchar(50)

AS

BEGIN

    DECLARE @SQL nvarchar(1000)

  

    IF OBJECT_ID (@TableName,'U') IS NOT NULL

    SET @SQL = 'INSERT INTO ' + @TableName + ' SELECT * FROM OPENDATASOURCE'

    ELSE

    SET @SQL = 'SELECT * INTO ' + @TableName + ' FROM OPENDATASOURCE'

 

    SET @SQL = @SQL + '(''Microsoft.ACE.OLEDB.12.0'',''Data Source='

    SET @SQL = @SQL + @FilePath + ';Extended Properties=''''Excel 12.0;HDR='

    SET @SQL = @SQL + @HDR + ''''''')...['

    SET @SQL = @SQL + @SheetName + ']'

    EXEC sp_executesql @SQL

END

GO

Hope this will help you.

  jayaram thoram replied to Radhika roy
18-Aug-11 10:13 AM
Thank you.
Create New Account
help
Problem with SQL Server Import and Export Wizard in SQL Server 200 SQL Server We are using SQL Server Import and Export Wizard to export from a view to a flat file. This
Converting a MS Access database to SQL Server SQL Server Does anyone know how to convert an MS Access database to SQL Server using SQL Server 2005? SQL Server Programming Discussions SQL Server 2005 (1) SQL Server (1) Database (1) Windows
Upsizing large Access table to SQL Server SQL Server I am using SQL Server 2005 Express and want to upsize large tables from MS Access to SQL Server. The upsizing wizard is skipping the large tables without any error. The QueryTimeout value in
migration from MS Access to SQL Server SQL Server I am migration a MS Access database to SQL server backend database. Are there any better way to import thewhole databases? Your help is great appreciated, SQL Server New Users Discussions SQL Server (1) SQL Server Books Online (1) SQL server backend
Attach SQL Server 2008 database to SQL Server 2000 Server SQL Server Hi, I have created a database in SQL Server 2008, with compatibility level of SQL Server 2000. Now when I detach the DB