I’ve used arguments exclusively to generate the correct SQL string for getting a dataset - to date. Mainly because there have been occassions when I just needed a single criteria in the ‘where’ clause and after putting that in a var I’ve struggled to get the ’ "’ " right for it to be seen as criteria.
Some help I was given today on setting valuelists prompted me to use this: var query = ‘SELECT * FROM mechanism_library WHERE cc_mech_type = "’ + mech_type + “”;
This was not acceptable and I tried allways to format the sequence of quotes to get my variable into the string, to no avail! When the text in mech_type was recognised (thru debug review) the getDataSetByQuery thought it was looking for a column by the name of the text in mech_type (‘Prevention’)!
What is the correct syntax for using a variable in the SQL string without passing arguments (that is just adding a variable to the string)?
Thanks Hans - I was aware of that challenge - but I was stupidly thinking small about my current situation where I create the variable content in code - its not user input.
Prepared statements are also faster in execution on the database, because the database only has to figure out once how to best execute the query.
As long as the SQL of the prepared statements is the same, the DB will think of it as the same statement, regardless the value of the arguments.
If you paste the values into the SQL string, then each query will be a different statement to the database and thus the database needs to figure out again how to best execute the query.
So in short: use prepared statements! It’s saver, faster and easier to write and maintain.
pbakker:
Prepared statements are also faster in execution on the database, because the database only has to figure out once how to best execute the query.
As long as the SQL of the prepared statements is the same, the DB will think of it as the same statement, regardless the value of the arguments.
If you paste the values into the SQL string, then each query will be a different statement to the database and thus the database needs to figure out again how to best execute the query.
So in short: use prepared statements! It’s saver, faster and easier to write and maintain.