Recently I have been writing parameterized queries like this:
SQL = " INSERT INTO myTable (column1, column2, column3) VALUES (?, ?, ?); "
Set newParameter = cmdConn.CreateParameter("@column1", ad_Integer, ad_ParamInput, Len(input1), input1)
cmdConn.Parameters.Append newParameter
Set newParameter = cmdConn.CreateParameter("@column2", ad_Integer, ad_ParamInput, Len(input2), input2)
cmdConn.Parameters.Append newParameter
Set newParameter = cmdConn.CreateParameter("@column3", ad_Integer, ad_ParamInput, Len(input3), input3)
cmdConn.Parameters.Append newParameter
And I was of the understanding, that where I’ve used @column1 when appending a new parameter, this was allocating that parameter to that particular column in the query… but have recently been informed otherwise. This then caused some confusion and have a couple of questions.
Take the above parameter, @column1. How does that parameter know that it’s going to be used in the first question mark (?) of the query? Do I have to append the parameters in the same order as the question marks, or doesn’t it matter which order? Also, if I wanted to add the users input in to two columns, do I use two parameters or can I use the same one?
Any help gratefully received. This has been on my mind for weeks, so really looking forward to an explanation 🙂
In parameterized queries, the database replaces the question marks with the parameters in the order in which they are added.
So column1 parameter will go into question mark 1, column2 parameter will go into question mark 2, and column3 parameter will go into question mark 3
For more check