+ Reply to Thread
Results 1 to 9 of 9

Run SQL code which is in a spreadsheet

  1. #1
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Run SQL code which is in a spreadsheet

    Ok, I don't really know how to title or categorize this one, but here it is..

    I have a spreadsheet with an SQL query in it. below is a super basic example;
    sqlonspreadsheet.PNG
    The query will always be one column wide, but can have tens or hundreds of thousands of rows (a lot of INSERT statements)
    What I want to do is run that query and dump the results into another sheet (within the same file)
    Is this possible? If so, what angle should I attack this from..?
    You should hit F5, because chances are I've edited this post at least 5 times.
    Example of Array Formulas
    Quote Originally Posted by Jacc View Post
    Sorry, your description makes no sense. I just made some formula that looks interesting cause I had nothing else to do.
    Click the * below on any post that helped you.

  2. #2
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: Run SQL code which is in a spreadsheet

    Double posted, please delete

    Note to mod: i solved the problem i reported on this post.
    Last edited by Speshul; 11-28-2014 at 12:01 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: Run SQL code which is in a spreadsheet

    A better way to see what I am trying to do is:



    sqlinexcel2.PNG
    I want to be able to type a column in there as my SQL statement.

    OR

    I want to use the contents of a Text File as my SQL statement (i can export the query in the cells to a text file with vba pretty easily)


    Hope that clarifies things a little...

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Run SQL code which is in a spreadsheet

    What's the database you are connecting to?
    Remember what the dormouse said
    Feed your head

  5. #5
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: Run SQL code which is in a spreadsheet

    It is a company intranet database, why do you ask?

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Run SQL code which is in a spreadsheet

    Because the connection string would depend on what type of database you are trying to access.

  7. #7
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: Run SQL code which is in a spreadsheet

    I have the server set up already in Data Sources (ODBC) if that's what you mean. I can pull results into excel using a query no problem, I am mostly looking for a way for the query itself to change on the fly.

    My query is generated with formulas, for example in B2 I would have:
    Please Login or Register  to view this content.
    Column A contains the numbers I need to match data for in SQL

    At the bottom of all the 'inserts' i have a select statement which joins my table to a few others and retrieves data relevant to the values in Column A.


    Normally, I would just copy all the rows of data in B and dump them into SMSS, then copy the results back to excel and index/match them all together, I am looking for a quicker way, and think that if Microsoft Query can look at column B's values, instead of a hard-coded query, it would save a lot of time.


    Hope this makes sense.

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Run SQL code which is in a spreadsheet

    I wouldn't use a querytable for that since you aren't returning data. I'd simply connect to the database using ADO (or DAO) and then execute the query you want. That requires the appropriate connection string for the database, be it Oracle or SQL Server or whatever.

  9. #9
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: Run SQL code which is in a spreadsheet

    I am returning data though;

    My query creates a temp table in SQL, inserts a list of values into that temp table, and then a select statement (within the same query) joins my temp table to numerous tables in the DB, returning the required data to match to the source. "MyTable" should actually be "#MyTable", didn't know if that was important.

    The MyTable is created each time the query runs, and is deleted each time the connection is closed.

+ 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. [SOLVED] VBA code to read a value in a spreadsheet and act accordingly
    By delaneybob in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-09-2012, 07:47 AM
  2. Error with Code within Spreadsheet'
    By mprice1988 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-27-2008, 08:38 AM
  3. Copy code to a new spreadsheet
    By Markl in forum Excel General
    Replies: 3
    Last Post: 03-29-2006, 11:00 AM
  4. [SOLVED] copy code with spreadsheet
    By Lee Hunter in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-20-2006, 01:40 PM
  5. [SOLVED] Can i see code and spreadsheet simultaneously
    By Stuart in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 08-26-2005, 11:05 AM

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