Use global variable in Select

Hi
i have a global variable, which is an array. In index 2 i stored a field name that i want to use in a grid in the select statement. Like that:

 SELECT * FROM table WHERE (`[rw_fields][2]` <> 0)

In [rw_fields][2] the fieldname in the table is stored, f.e. ‘dbvalue’. So the Select Statement should be like that:

 SELECT * FROM table WHERE (`dbvalue` <> 0)

Question: How can i put the content of my array into a select Statement?

Best regards
Steve

I do not think it would work directly, but you can use intermediate variables for that. I you look at sample code (right panel in editor you will find how SC is reading record data into an array. It seems that you just need to reverse the same process.

Hi
This is not possible in the SQL Section of a grid…

have you tried to something like:

  $my_var = ([rw_fields][2])
SELECT * FROM table WHERE (`$my_var` <> 0) 

I’am not can put PHP Code in the SQL Select Statement Section …

You can insert this code in onScriptInit Event

[glo_my_var] = ([rw_fields][2]);

then use SQL Select Section

SELECT * FROM table WHERE ({glo_my_var} <> 0)

SELECT * FROM table WHERE ([glo_my_var] <> 0)

Cool, thank’s. Like that it worked!

I have a different situation, my table name is dynamically generated and I use the Union in the SQL for the Grid Application. Is there a way to include the global variable to form the table name?

For e.g. my grid application’s SQL will be like this (both the tables are having same structure

SELECT
*
FROM
table_1_[global_variable]
UNION
SELECT
*
FROM
table_2_[global_variable]