+ Reply to Thread
Results 1 to 2 of 2

Variable in postgreSQL query

  1. #1
    Registered User
    Join Date
    11-05-2013
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    44

    Variable in postgreSQL query

    Hi, I need to do a couple of things, first I need to connect my DB to excel, and I achieve that using a add-in.
    but I have a problem with the queries, let's make an example.

    Let's say in cell A1, I'd like to perform the query SELECT date FROM time WHERE id = 1
    I achieve that without any problem, but what about when I have to change id?
    Ideally I want to have in A2 the id (so I can change at glance) and in A3 the query updated.
    How can I do that?

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: Variable in postgreSQL query

    Have you looked at MS-Query? It is part of Excel (Data -> From Other Sources -> MS Query).

    What I recommend is work out the SQL that you want using WinSQL or some other tool. Hard code any restrictions that don't change into the SQL. Hard code the parameters you wish to pass with temporary values.

    Then open MS-Query and turn off the wizard. Make the connection to the database you want. This will bring up a list of tables in the database. Close this list.

    Click on the SQL button. Copy and paste your SQL into the window. If the query is simple enough, it will display graphically. You will see the various where clauses in the criteria area of the graphical query outline. For those queries you wish to pass at runtime, replace the hard coding with [Prompt Text]. This will prompt you for an entry for this field when the query is run.

    Click on the exit door icon and the data will be returned to the workbook. If you right click in the returned data area, you get a dropdown menu. One of the items is parameters. Select that item and one of the options is to get the value from a cell on the spreadsheet. This cell could contain a formula, or a data validation of allowed values or whatever you want.

    If the query is complex and cannot be displayed graphically, then use the technique in this article: http://www.utteraccess.com/wiki/index.php/MS_Query.

    This process rewrites the SQL code and substitutes it for the connection string. It is not limited to just normal parameters. I've used it to switch between database tables or add a dynamic list of models for an IN clause generated elsewhere on the spreadsheet. In other words, I don't reprogram my SQL, I fill out data on the spreadsheet and it rewrites the code for me.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

+ 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. A report in excel which pulls data from postgresql db
    By bhavikumehta in forum Access Tables & Databases
    Replies: 1
    Last Post: 04-20-2013, 12:27 PM
  2. SQL Query Return One Variable
    By yawnzzzz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-11-2011, 12:34 AM
  3. Variable Database Query
    By tomlancaster in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-17-2009, 07:20 AM
  4. vba query simple variable
    By mgkmn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-12-2007, 07:01 PM
  5. Database query variable
    By brocklanders in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-15-2006, 12:44 PM
  6. Excel Query Variable
    By Kinesthesia in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-12-2006, 03:25 PM
  7. [SOLVED] With QUERY how to input a variable
    By Guillaume in forum Excel General
    Replies: 1
    Last Post: 12-08-2005, 10:45 AM
  8. [SOLVED] put query result into variable
    By andy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-24-2005, 03: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