Right way for passing parameters for IN-subquery (classic ASP, ADODB.Command)

149 Views Asked by At

good afternoon

i'm going crazy with passing parameters to a very simple query with a subquery; i wrote hundreds of complicated queries, i'm afraid my fault is just in passing parameters to the subquery or everything else seems very normally manageable

making things simpler i prepare my query this way:

Set myConn = Server.CreateObject("ADODB.Connection")
myConn.Mode = 3 '3 = adModeReadWrite
myConn.Open MM_DEFDB_STRING
Set myCmd = Server.CreateObject("ADODB.Command")
myCmd.ActiveConnection = myConn
myCmd.CommandType = 1 '1 = adCmdText
myCmd.CommandText = strQuery 'this is my query with ? placeholders
myCmd.Prepared = false 'i won't repeat it
Set myRs   = Server.CreateObject("ADODB.Recordset")
myRs.CursorType = 1 '0 = adOpenForwardOnly, 1 = adOpenKeyset, ...
myRs.LockType = 3 '3 = adLockOptimistic, ...

(here i prepare query and parameters)

myRs.Open myCmd

this should be correct, it is always the same for a lot of queries

1st case:

strQuery = SELECT * FROM TableA WHERE TableA.ID IN (SELECT myIDs FROM TableB WHERE TableB.NumericField=?) AND StringField=?

then i use this command cycling on two arrays, one for parameter values, another for types:

myCmd.Parameters.Append myCmd.CreateParameter("@Par" & i, VTypes(i), 1, 0, VValues(i))

here VValues is [8975, "011M1005D"] and VTypes is [3, 8]

result is 12 records, it is correct, i see those records on my database

2nd case (i only changed order of my two AND clauses):

strQuery = SELECT * FROM TableA WHERE StringField=? AND TableA.ID IN (SELECT myIDs FROM TableB WHERE TableB.NumericField=?)

VValues is ["011M1005D", 8975] and VTypes is [8, 3]

result is ZERO records, not an error but simply no records WHY in your opinion ??? i would expect the same 12 records as before, the AND logic operator is simmetric!

3rd case (i use values on 2nd case instead of parameters):

strQuery = SELECT * FROM TableA WHERE StringField='011M1005D' AND TableA.ID IN (SELECT myIDs FROM TableB WHERE TableB.NumericField=8975)

result is correct: 12 records

curious, with plain values the order seems not important anymore

i also tried to debug the parameters collection, printing for each parameter it's value and it's type in order to check them just a line before the Open commmand: they seems correct and in the correct order in both cases

you are last chance or i have to rearrange a lot of code using some JOIN (but i really would understand my mistake before!). thank you for patience

0

There are 0 best solutions below