Subscribe Bookmark RSS Feed

Making a JMP variable part of a SQL query?

rleeper

Community Trekker

Joined:

Jun 3, 2014

Hi All.

I would like to add the number of days of records to pull from my SQL database.

I am currently using :

WHERE TestTime > GetDate()-4

I would like to replace the number 4 with a variable that the user can input from a dialog box.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Solution

nw = new window("My Query", << modal(),

    panelbox("Input",

        hlistbox(

            text box("Enter the number of days: "),

            ndays_teb = text edit box()

        ),

    ),

    panelbox("Actions",

        hlistbox(

            ok_button = button box("OK",

                        ndays = ndays_teb << get text;

                        ok_pushed = 1;),

            cancel_button = button box("Cancel", ok_pushed = 0),

        )

    )

);

sql_statement = "SELECT '" || ndays || "' FROM DUAL";

7 REPLIES
Solution

nw = new window("My Query", << modal(),

    panelbox("Input",

        hlistbox(

            text box("Enter the number of days: "),

            ndays_teb = text edit box()

        ),

    ),

    panelbox("Actions",

        hlistbox(

            ok_button = button box("OK",

                        ndays = ndays_teb << get text;

                        ok_pushed = 1;),

            cancel_button = button box("Cancel", ok_pushed = 0),

        )

    )

);

sql_statement = "SELECT '" || ndays || "' FROM DUAL";

rleeper

Community Trekker

Joined:

Jun 3, 2014

PMroz,

I understand the text box input.  I'm not following the SQL statement.

"SELECT * FROM dbo.QVvw_Power_Measurements WHERE TestDate > GetDate()-4",

is my original.

So are you saying something like this?

"SELECT * FROM dbo.QVvw_Power_Measurements WHERE TestDate > GetDate() '" || ndays || "'";

???

pmroz

Super User

Joined:

Jun 23, 2011

Almost.  I was showing an example that would work in Oracle SQL.  Your example looks like SQL Server.  Try this:

"SELECT * FROM dbo.QVvw_Power_Measurements WHERE TestDate > GetDate()-" || ndays;

rleeper

Community Trekker

Joined:

Jun 3, 2014

PMroz,

Works!  Thank you so much!

Leep

rleeper

Community Trekker

Joined:

Jun 3, 2014

PMroz,

So when I try to use the variable as a text input the query stops working.  The variable is a text string "R8D-01" I enter with the input box.  nlot is the variable.

"SELECT * FROM dbo.tbl_NVR WHERE LT_NAME = " || nlot,

Jmp shows the proper value when I hover over the nlot variable. i.e. "R8D-01"

If I type out

"SELECT * FROM dbo.tbl_NVR WHERE LT_NAME = 'R8D-01' "

I get the data.

pmroz

Super User

Joined:

Jun 23, 2011

You forgot to put single quotes around it.  Like this:

sql_statement = "SELECT * FROM dbo.tbl_NVR WHERE LT_NAME = '" || nlot || "'";

// or use evalinsert for a cleaner look.  Note the use of ^^ around the nlot variable

sql_statement = evalinsert(

"SELECT * FROM dbo.tbl_NVR WHERE LT_NAME = '^nlot^'");

rleeper

Community Trekker

Joined:

Jun 3, 2014

PMroz,

You sir, are a genius!

Sorry I'm taking so much of your expert time.  Thank you for all the help.