ADO Parameterized Queries with Subqueries Error

1

I have a legacy classic ASP application running with SQL Server 2012 (also tested with 2016) that I am trying to switch over to using parameterized queries. All the site's queries run through a function which expects a sql statement as a string with parameters represented by question marks as well as an array of those parameters. The function currently filters the parameters to make them sql safe and puts them into the sql string before executing the statement.

Given this, I thought it would be pretty straightforward to switch this to parameterized queries. Initial testing looked good, and everything appeared to be working properly until I hit a sql statement with parameters in subqueries.

Here's a test sample of what works:

Const connectionString = "Provider=SQLNCLI11; Server=********; Database=********; UID=*******; PWD=*******"

Dim sql, productId, parameters
sql = "SELECT SKU FROM Products WHERE ProductId = ?"
productId = 3
parameters = Array(productId)

Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connectionString

Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = sql
cmd.Parameters.Refresh

Dim rs
Set rs = cmd.Execute(, parameters)

Response.Write("SKU: " & rs("SKU"))

No problem, this returns the SKU as expected. However, if I use a subquery:

Const connectionString = "Provider=SQLNCLI11; Server=********; Database=********; UID=*******; PWD=*******"

Dim sql, productId, parameters
'contrived subquery for demonstration purposes
sql = "SELECT SKU FROM ( SELECT SKU FROM Products WHERE ProductId = ? ) AS P"
productId = 3
parameters = Array(productId)

Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connectionString

Dim cmd
Set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = sql
cmd.Parameters.Refresh

Dim rs
Set rs = cmd.Execute(, parameters)

Response.Write("SKU: " & rs("SKU"))

It throws an error on the cmd.Parameters.Refresh line:

Microsoft VBScript runtime error '0x80004005' Microsoft SQL Server Native Client 11.0 Syntax error, permission violation, or other nonspecific error

If I check cmd.Parameters.Count in the first sample, I correctly get 1. In the bad sample it throws the same error.

Is there any explanation as to why putting the parameter into a subquery causes problems with the parameter collection? I did try manually adding the parameter to the Parameters collection, and that works fine, but it means modifying hundreds of existing sql calls, so for the moment the cmd.Parameters.Refresh round-trip was worth the expense.

sql-server
asp-classic
ado
parameterized-query
asked on Stack Overflow Nov 25, 2019 by Dave

1 Answer

-1

You can give cmd.execute what you want, but I haven't used it in a long time.

cmd.execute("SELECT SKU FROM ( SELECT SKU FROM Products WHERE ProductId = ? ) AS P", Array(productId))

answered on Stack Overflow Nov 27, 2019 by omerix

User contributions licensed under CC BY-SA 3.0