+ Reply to Thread
Results 1 to 20 of 20

Macro button to export excel spreadsheet as SQL file

  1. #1
    Registered User
    Join Date
    04-13-2015
    Location
    Luanda
    MS-Off Ver
    Vista
    Posts
    43

    Post Macro button to export excel spreadsheet as SQL file

    Hi everyone,
    I'm new here and I would love to learn from everyone the secrets of excel.
    I have an assignment from my job which is to create a macro button to generate a SQL file from a spreadsheet.
    The workbook contains different s sheets and each one contains tables to be exported.

    I want help to create this button which generates a SQL file from a particular sheet, any I chose (ex: export the values from the Location_sheet - all the data at once, without having to write it down the rows but the columns only, like SITEID, LOCATION, DESCRIPTION).

    I hope you guys can understand me

    Please support.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Macro button to export excel spreadsheet as SQL file

    You might find this helpful in getting started

    http://exceluser.com/formulas/msquer...ional-data.htm
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    04-13-2015
    Location
    Luanda
    MS-Off Ver
    Vista
    Posts
    43

    Re: Macro button to export excel spreadsheet as SQL file

    i think this is not what i need.
    ex: i have in one spreadsheet 5 columns (SITEID, PM, JP NUMBER, SEQUENCE) with 9000 rows each.
    so, I think i should created a insert into statement in order to create a sql file, but i want a button to press on, which can take all this information into one sql file.

    can you support me with that?
    Last edited by Isaacadete; 04-14-2015 at 10:20 AM.

  4. #4
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Macro button to export excel spreadsheet as SQL file

    well, I think it's not clear what you mean by sql file...
    File with sql extension e.g. your_file.sql is actually a text file woth some sql statement in it. However this sql statement can be different type - e.g. to retrieve data (i.e. SELECT), insert data (INSERT), delete, etc.
    In any case you (and we) need info for your file and also for the DB/table.
    Note that you can interact with DB directly from excel using ADO, no need to create sql file and execute it afterwards...
    Last edited by buran; 04-14-2015 at 10:14 AM.
    If you are pleased with a member's answer then use the Star icon to rate it.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Macro button to export excel spreadsheet as SQL file

    I think it's pretty clear what you're wanting, but we could use a sample file, we also need to know what database it is

  6. #6
    Registered User
    Join Date
    04-13-2015
    Location
    Luanda
    MS-Off Ver
    Vista
    Posts
    43

    Re: Macro button to export excel spreadsheet as SQL file

    Buran
    I will try to upload my file here,
    But, in case I want to upload a table information to Oracle SQL from excel
    I have column name and the values. What should I do?
    it would be possible to create a button to extract the file as SQL? or should I just write a insert statement with columns name and mentioning the values? Because I have a table with 5 columns and thousands of rows.

    Please support
    Last edited by Isaacadete; 04-15-2015 at 03:00 AM.

  7. #7
    Registered User
    Join Date
    04-13-2015
    Location
    Luanda
    MS-Off Ver
    Vista
    Posts
    43

    Re: Macro button to export excel spreadsheet as SQL file

    Buran
    I will try to upload my file here,
    But, in case I want to upload a table information to Oracle SQL from excel
    I have column name and the values. What should I do?
    it would be possible to create a button to extract the file as SQL? or should I just write a insert statement with columns name and mentioning the values? Because I have a table with 5 columns and thousands of rows.

    Please support

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Macro button to export excel spreadsheet as SQL file

    tbh it's not really worth creating a button, just create a formula, drag it down and copy and paste it. That's what I do

  9. #9
    Registered User
    Join Date
    04-13-2015
    Location
    Luanda
    MS-Off Ver
    Vista
    Posts
    43

    Re: Macro button to export excel spreadsheet as SQL file

    This is the formula I developed

    Insert into Oracle.Location (SITEID, FAMILY, SEQUENCE, DESCRIPTION) values ('blck 18','alfa','90','beginner')

    is it correct?

    "Location" is the spreadsheet,
    "SITEID" and the others are the columns
    "blck 18" and the others are the values in the columns

    I wonder if I should change the Oracle as the SQL software to dbo.

    eg: insert into dbo.location or insert into [dbo].[Location]?
    Last edited by Isaacadete; 04-15-2015 at 04:36 AM.

  10. #10
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Macro button to export excel spreadsheet as SQL file

    Numbers aren't usually strings so you wouldn't enclose them, but it depends on the datatype of the field - so noone can answer you

  11. #11
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Macro button to export excel spreadsheet as SQL file

    Hi
    sorry for late reply.
    You have two options:
    one is to create text file with sql extension which will have INSERT statements, one per every row
    second is to use ADO and execute sql command that will insert the data in your Oracle DB. Here is example that does the same with Access DB. http://www.excelforum.com/showthread...5170&p=4032216
    Note that you will need to change the connection string based on what driver e.g. ODBC driver you have installed. You can google the exct connection string.

    Regarding your INSERT statement above - you must have ; at the end e.g.

    Please Login or Register  to view this content.
    Repalce OracleTable with real table name in your DB

  12. #12
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Macro button to export excel spreadsheet as SQL file

    @buran, what's wrong with copy and paste?

  13. #13
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Macro button to export excel spreadsheet as SQL file

    Quote Originally Posted by Kyle123 View Post
    @buran, what's wrong with copy and paste?
    @Kyle123 - don't know what you mean

  14. #14
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Macro button to export excel spreadsheet as SQL file

    You simply put the formula in each, row, drag down and copy and paste the column into your SQL editor and execute it. VBA is a bit overkill here

  15. #15
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Macro button to export excel spreadsheet as SQL file

    Ah, I see what you mean. Nothing wrong in that respect if it's one time task and OP has access to some sql editor/db management tool.
    if it's one time task he can also export data as csv and import the data from there (at least in MySQL and Access it's possible and I believe same is true for oracle)

  16. #16
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Macro button to export excel spreadsheet as SQL file

    It's typically painful to do with server based databases since typically the user and the database will not both have access to the same files. So to do it as csv, whilst possible is usually painful and a pain.

    Copying and pasting is a doddle though, the OP has access to a SQL editor presumably since he has written the sql to go in it and is doing something with the SQL after creating it.

    Even if it's not a one time task, it's still easy, especially with tables that copy the formula down to the bottom of the data

  17. #17
    Registered User
    Join Date
    04-13-2015
    Location
    Luanda
    MS-Off Ver
    Vista
    Posts
    43

    Re: Macro button to export excel spreadsheet as SQL file

    Dears, I've decided to use the string:
    problem: I have to many characters, so I have to use CONCATENATE but its not working, any suggestion?

    This is the code

    = CONCATENATE ("insert into ORACLE.LOCATION (FIELDNAME, SITEID, LOCATION, DESCRIPTION, (M)TYPE, PARENT, FAILURECODE DESCRIPTION, FAILURECODE, SAFETY CRITICAL (FL010), TAG CRITICALITY (FL08), EX REGISTER (FL02), ANGILLEVEL, GLACCOUNT, ANGENGSYSID, SYSTEMID, (M)STATUS, ANGDRAWNUMBER, GRID REF (FL09), PHYSICAL AREA (FL03), MAINTENANCE JUSTIFICATION, ADOPTED STRATEGY (L02)) CONCATENATE ('" & B4 & "','"& C4 & "','" & D4 & "','" & E4 & "','" & F4& "','"& G4 & "','" & H4& "','" & I4 & "', '" & J4 &"', '" & K4 &"', '" & L4 & "', '" & M4 & "', '" & N4& "', '" & O4 & "' '" & P4 & "','" & Q4 & "','" & R4 & "', '" & S4 & "','" & T4 & "','" & U4 & "','" & V4 & "')")

  18. #18
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Macro button to export excel spreadsheet as SQL file

    Why do you need to use CONCATENATE? It's the single most useless function in Excel (I reckon).

    What "isn't working"?

    Try:
    Please Login or Register  to view this content.
    Last edited by Kyle123; 04-15-2015 at 08:57 AM.

  19. #19
    Registered User
    Join Date
    04-13-2015
    Location
    Luanda
    MS-Off Ver
    Vista
    Posts
    43

    Re: Macro button to export excel spreadsheet as SQL file

    using INSERT INTO
    its says: Text values in formulas are limited to 255 characters. to create text values longer than 255 characters in a formula, use the CONCATENATE function or the concatenation operator (&)

    So I used the CONCATENATE but is gives me the same error

  20. #20
    Registered User
    Join Date
    04-13-2015
    Location
    Luanda
    MS-Off Ver
    Vista
    Posts
    43

    Re: Macro button to export excel spreadsheet as SQL file

    I'ts working!
    Thanks so much for you support. Thanks a lot. Any more doubt I will reach you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel 2010, macro button to export data to another workbook
    By BennyBlueHill in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-18-2013, 05:01 PM
  2. Replies: 2
    Last Post: 07-18-2012, 11:17 PM
  3. Export EXCEL spreadsheet to .csv file
    By nmacneil@us.ibm.com in forum Excel General
    Replies: 5
    Last Post: 06-08-2006, 09:40 AM
  4. Can I export an excel spreadsheet to a .csv file?
    By nmacneil@us.ibm.com in forum Excel General
    Replies: 6
    Last Post: 06-07-2006, 06:35 PM
  5. Replies: 1
    Last Post: 03-01-2005, 05:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1