Python - Parameters in ADO queries
- 6 years ago
I'm looking through some code that I have for SQL queries and I am using a named query parameter in the SQL string.
For example, I have the following query
SELECT COUNTERID FROM AUTOMATIONCOUNTERS (NOLOCK) WHERE NAME = :NAME
I use, then the ADO.CreateADOCommand method to generate a command object. I set the CommandText property of that object to the query as indicated. The command object has a Parameters collection with a method called ParseSQL which builds the parameters collection based upon what's in the query. You can then use the ParamByName method on that same collection to grab the parameter and assign the value.
So... rough pseudocode looks like this
var localCommand var SQLString SQLString = 'SELECT COUNTERID FROM AUTOMATIONCOUNTERS( NOLOCK) WHERE NAME = :NAME' localCommand = ADO.CreateADOCommand(); localCommand.CommandText = SQLString; localCommand.Parameters.ParseSQL(SQLString, true); localCommand.Parameters.ParamByName('NAME') = 'TestCounter'
To execute, you just need to make sure you assign a proper active connection to the command object and then call the Execute method on the command object.
This works very well for me... I have, actually, a complex ScriptExtension that does all this for me.
Hope this helps.