Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL is creating a table instead of a file - whats wrong with my SQL-String

SQL is creating a table instead of a file - whats wrong with my SQL-String

Scheduled Pinned Locked Moved Database
databasesysadminquestion
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F fracalifa

    Hi all, I want to export my data from a SQL-Server table to an Excel-File. The following code shows my routine.

    oConnection.ConnectionString = "Provider=SQLOLEDB; Server=Servername; database=ABC; Trusted_Connection=yes; User ID=ABC_MAN; Password=secretABC";
    oConnection.Open();
    oCmd.Connection = oConnection;
    oCmd.CommandTimeout = 60;
    oCmd.CommandText = "SELECT id AS NO, name AS NAME INTO [Excel 8.0;DATABASE=Test.xls] FROM MyDataTable WHERE typ=1";
    oCmd.ExecuteNonQuery();

    But instead of an exel file the string creates a new table with the name Excel 8.0;DATABASE=Test.xls Where is my fault ? Different examples has shown that this should work :confused: Thanks Frank

    B Offline
    B Offline
    Bassam Saoud
    wrote on last edited by
    #2

    The SQL SELECT INTO statement is used to select data from a SQL database table and to insert it to a different table at the same time. Tutorial[^]

    F 1 Reply Last reply
    0
    • B Bassam Saoud

      The SQL SELECT INTO statement is used to select data from a SQL database table and to insert it to a different table at the same time. Tutorial[^]

      F Offline
      F Offline
      fracalifa
      wrote on last edited by
      #3

      Thank you, I know what SQL normally does ... but I think you don't know what I am expecting from this query ... Thank Frank

      B 1 Reply Last reply
      0
      • F fracalifa

        Thank you, I know what SQL normally does ... but I think you don't know what I am expecting from this query ... Thank Frank

        B Offline
        B Offline
        Bassam Saoud
        wrote on last edited by
        #4

        BCP [^] into a CSV file

        1 Reply Last reply
        0
        • F fracalifa

          Hi all, I want to export my data from a SQL-Server table to an Excel-File. The following code shows my routine.

          oConnection.ConnectionString = "Provider=SQLOLEDB; Server=Servername; database=ABC; Trusted_Connection=yes; User ID=ABC_MAN; Password=secretABC";
          oConnection.Open();
          oCmd.Connection = oConnection;
          oCmd.CommandTimeout = 60;
          oCmd.CommandText = "SELECT id AS NO, name AS NAME INTO [Excel 8.0;DATABASE=Test.xls] FROM MyDataTable WHERE typ=1";
          oCmd.ExecuteNonQuery();

          But instead of an exel file the string creates a new table with the name Excel 8.0;DATABASE=Test.xls Where is my fault ? Different examples has shown that this should work :confused: Thanks Frank

          S Offline
          S Offline
          scottgp
          wrote on last edited by
          #5

          It looks like you want something like this: INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 8.0;Database=C:\testing.xls;', 'SELECT Name, Email FROM [Sheet1$]') SELECT Name, Email FROM tblnames or 1 Export data to existing EXCEL file from SQL Server table insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\testing.xls;', 'SELECT * FROM [SheetName$]') select * from SQLServerTable I'm not sure of the correct syntax since I haven't used it before. I found it at after searching for "sql server export to excel [Excel 8.0;DATABASE=Test.xls]" Scott

          F 1 Reply Last reply
          0
          • F fracalifa

            Hi all, I want to export my data from a SQL-Server table to an Excel-File. The following code shows my routine.

            oConnection.ConnectionString = "Provider=SQLOLEDB; Server=Servername; database=ABC; Trusted_Connection=yes; User ID=ABC_MAN; Password=secretABC";
            oConnection.Open();
            oCmd.Connection = oConnection;
            oCmd.CommandTimeout = 60;
            oCmd.CommandText = "SELECT id AS NO, name AS NAME INTO [Excel 8.0;DATABASE=Test.xls] FROM MyDataTable WHERE typ=1";
            oCmd.ExecuteNonQuery();

            But instead of an exel file the string creates a new table with the name Excel 8.0;DATABASE=Test.xls Where is my fault ? Different examples has shown that this should work :confused: Thanks Frank

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            I found this link[^], which may be of help.

            F 1 Reply Last reply
            0
            • S scottgp

              It looks like you want something like this: INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 8.0;Database=C:\testing.xls;', 'SELECT Name, Email FROM [Sheet1$]') SELECT Name, Email FROM tblnames or 1 Export data to existing EXCEL file from SQL Server table insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\testing.xls;', 'SELECT * FROM [SheetName$]') select * from SQLServerTable I'm not sure of the correct syntax since I haven't used it before. I found it at after searching for "sql server export to excel [Excel 8.0;DATABASE=Test.xls]" Scott

              F Offline
              F Offline
              fracalifa
              wrote on last edited by
              #7

              Thank you, have tried your suggestions, but got some OLEDB-unknown errors, what ever that meens. (But I think,(not sure) the disadvantage is that the Excel-table must already exist) But thanks alot Frank

              1 Reply Last reply
              0
              • L Lost User

                I found this link[^], which may be of help.

                F Offline
                F Offline
                fracalifa
                wrote on last edited by
                #8

                Hi Richard, it's an interesting link - I will convert the code to C# - will take some minutes ;P I will let you know the result. Tnx Frank

                1 Reply Last reply
                0
                • F fracalifa

                  Hi all, I want to export my data from a SQL-Server table to an Excel-File. The following code shows my routine.

                  oConnection.ConnectionString = "Provider=SQLOLEDB; Server=Servername; database=ABC; Trusted_Connection=yes; User ID=ABC_MAN; Password=secretABC";
                  oConnection.Open();
                  oCmd.Connection = oConnection;
                  oCmd.CommandTimeout = 60;
                  oCmd.CommandText = "SELECT id AS NO, name AS NAME INTO [Excel 8.0;DATABASE=Test.xls] FROM MyDataTable WHERE typ=1";
                  oCmd.ExecuteNonQuery();

                  But instead of an exel file the string creates a new table with the name Excel 8.0;DATABASE=Test.xls Where is my fault ? Different examples has shown that this should work :confused: Thanks Frank

                  N Offline
                  N Offline
                  Niladri_Biswas
                  wrote on last edited by
                  #9

                  Try this

                  Create an Excel file named testing having the headers same as that of table columns and use this query

                  insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                  'Excel 8.0;Database=D:\testing.xls;',
                  'SELECT * FROM [SheetName$]') select * from SQLServerTable

                  To export data from Excel to new SQL Server table,

                  select *
                  into SQLServerTable FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                  'Excel 8.0;Database=D:\testing.xls;HDR=YES',
                  'SELECT * FROM [Sheet1$]')

                  To export data from Excel to existing SQL Server table,

                  Insert into SQLServerTable Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                  'Excel 8.0;Database=D:\testing.xls;HDR=YES',
                  'SELECT * FROM [SheetName$]')

                  Obtained from SQL Server Forums - Export to Excel Hope this helps. :)

                  Niladri Biswas

                  F 1 Reply Last reply
                  0
                  • N Niladri_Biswas

                    Try this

                    Create an Excel file named testing having the headers same as that of table columns and use this query

                    insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                    'Excel 8.0;Database=D:\testing.xls;',
                    'SELECT * FROM [SheetName$]') select * from SQLServerTable

                    To export data from Excel to new SQL Server table,

                    select *
                    into SQLServerTable FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                    'Excel 8.0;Database=D:\testing.xls;HDR=YES',
                    'SELECT * FROM [Sheet1$]')

                    To export data from Excel to existing SQL Server table,

                    Insert into SQLServerTable Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                    'Excel 8.0;Database=D:\testing.xls;HDR=YES',
                    'SELECT * FROM [SheetName$]')

                    Obtained from SQL Server Forums - Export to Excel Hope this helps. :)

                    Niladri Biswas

                    F Offline
                    F Offline
                    fracalifa
                    wrote on last edited by
                    #10

                    Thank you, I also found this example, but the examples assume, that an Excel file already exists. But I remember, 5 years ago, I could generate a new file with the sql-sting above (within VB6) without an additional action. Why can't I do that today - or how can I do that today ? Tnx for your time Frank

                    1 Reply Last reply
                    0
                    • F fracalifa

                      Hi all, I want to export my data from a SQL-Server table to an Excel-File. The following code shows my routine.

                      oConnection.ConnectionString = "Provider=SQLOLEDB; Server=Servername; database=ABC; Trusted_Connection=yes; User ID=ABC_MAN; Password=secretABC";
                      oConnection.Open();
                      oCmd.Connection = oConnection;
                      oCmd.CommandTimeout = 60;
                      oCmd.CommandText = "SELECT id AS NO, name AS NAME INTO [Excel 8.0;DATABASE=Test.xls] FROM MyDataTable WHERE typ=1";
                      oCmd.ExecuteNonQuery();

                      But instead of an exel file the string creates a new table with the name Excel 8.0;DATABASE=Test.xls Where is my fault ? Different examples has shown that this should work :confused: Thanks Frank

                      T Offline
                      T Offline
                      Tripathi Swati
                      wrote on last edited by
                      #11

                      if u dont want to use openrowset or bcp then best way is use string builder and then response.write with

                      response.AppendHeader("Content-Type", "application/vnd.ms-excel");

                      here you can give file name n storage location too.

                      Reasons are not Important but Results are Important. Swati Tripathi

                      modified on Friday, November 20, 2009 3:05 AM

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups