SQL 2 Excel - Variable Substitution - How-to PDF Print E-mail

You can use substitution variables in your Header / Footer and you SQL query to create dynamic reports. For instance, you can select things from your database depending on who is currently logged in. There is a long list of variables that you can use. Please see the SQL 2 Excel - Variable Substitution - Reference for details.

Header Example

Let's say we want to add a date to the header so that you can tell when the report was generated. This header specification will dynamically fill in the date/time when it gets executed:

Latest Articles as of {M} {j} {Y} - {H}:{i}:{s}

Results in a header, like this:

sql2excel_header_ex

SQL Query Example


The query can be made dynamic by putting substitution variables in it. This query returns a list of articles that the current user has created. The variable {USER_ID} is substituted with the current users ID at the time of execution.
SELECT ID, TITLE
FROM jos_content
WHERE created_by={USER_ID}

Please note that if this report is executed by a user who is not logged in then the value of {USER_ID} is 0 => no results are returned.

Turning Off Variable Substitution


The variable substitution is on by default. You may in some cases want to turn off if the variables clashes with actual data/strings that you wish to output. You can turn off variable substitution by going to the SQL 2 Excel Component in the Administrator. Click on the Parameters button in the SQL 2 Excel Toolbar.

sql2excel_parms_button

This brings up a small dialog where you can turn off variabale substitution in the Header/Footer and in the SQL Query.

sql2excel_parms_dlg_subst

 

Comments (5)add comment

wtfbrb said:

wtfbrb
...
For anyone out there struggling with substituting a string it is best to put the quotes in the query:
Select memberID,Address,City,State,Zip from my_members where state='{state}'

You can also add some wild cards as well in the example below to search for part of a title:

SELECT ID, TITLE
FROM jos_content
WHERE TITLE='%{SEARCH_TITLES}%'
 
July 30, 2009
Votes: +0

wtfbrb said:

wtfbrb
...
Another thing that throws an error on your worksheet but I find very handy is for the multi select you can use the following:

SELECT * FROM 201007_awa
WHERE b_state IN ({s_state})
 
August 03, 2010
Votes: +0

Mats @ Jommla-R-Us said:

admin
...
@wtfbrb: The reason it throws an error in the syntax check is because the value of the Workbook Parameter substitution variable is not known within the context of a Worksheet. It could be a number, a string or a list of values etc etc. A Worksheet could possible be used in multiple Workbooks and what value the Workbook parameter should have is not assigned until you download a Workbook.
 
August 03, 2010
Votes: +0

John McCall said:

0
...
Is there any way to substitute the results of a query in a header. I report for an event and define a substitution variable {EV_ID} to use in the query. How can I get the event name in the header? It would be defined by something like SELECT event_name FROM jos_events WHERE event_id = '{EV_ID}'

If this can't be done, might it be added?
 
November 23, 2010
Votes: +0

John McCall said:

0
...
Please ignore the above - I figured it out

NICE Product!!!
 
November 23, 2010
Votes: +0

Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.

busy
 
 
  Design by augs-burg.de & go-vista.de  
 
     
 
   
 

The Joomla!(R) name is used under a limited license from Open Source Matters in the United States and other countries. http://Joomla-R-Us.com is not affiliated with or endorsed by Open Source Matters or the Joomla! Project.