|
This macro allows the user to execute SQL commands and returns the result to the "dataset" variable. The "dataset" structure is an array (line/column).
The "connection" parameter is optional. Use when the command is executed in a database different from that specified for the application. Note: The connection parameter does not accept variable. You must enter the name of the connection that the SQL command will execute.
Ex. 1: sc_lookup(dataset, "select customer_id, customer_name, credit_limit from customers" );
To have access to the first line (Dataset), use : {customer_id} = {dataset[0][0]}; {customer_name} = {dataset[0][1]}; {credit_limit } = {dataset[0][2]};
To have access to the second line (Dataset), use: {customer_id} = {dataset[1][0]}; {customer_name} = {dataset[1][1]}; {credit_limit} = {dataset[1][2]};
If occurs error in the execution of the SQL command, the variable attributed to the dataset will return as "false" and the error message will be available in the "dataset_error" variable. It is also important to verify the select returned data, to prevent access to non-existent variables, once the output array only will be created if the select command returns data.
Ex. 2: sc_lookup(my_data, "select customer_id, customer_name, credit_limit from customers"); if ({my_data} === false) { echo "Access error. Message=". {my_data_error} ; } elseif (empty({my_data})) { echo "Select command didn't return data"; } else { {customer_id} = {my_data[0][0]}; {customer_name} = {my_data[0][1]}; {credit_limit} = {my_data[0][2]}; }
Ex. 3: The SQL command also can be composed of application fields (local variables) or of global variables: sc_lookup(dataset, "select order_value from orders where clientid = '{customer_id} ' and salesman_id = [var_glo_salesman]");
Note: The command must always be finished with a semicolon ";".
Note2: For a big result returned in the dataset we recommend the use of the macro sc_select instead of this one.
|