ASP Classic Named Parameter in Paramaterized Query: Must declare the scalar variable

后端 未结 4 1538
终归单人心
终归单人心 2020-12-06 22:05

I\'m trying to write a parameterized query in ASP Classic, and it\'s starting to feel like i\'m beating my head against a wall. I\'m getting the following error:

相关标签:
4条回答
  • 2020-12-06 22:37

    ADO is going to expect question marks instead of actual parameter names in this case. Right now, the SQL "select @something" is not actually parameterized: it sees the "@something" as an (undeclared) SQL variable, not as a parameter. Change your CommandText line to this:

    cmd.CommandText = "select ?"
    

    And I think you will get the result you are looking for.

    Good luck!

    0 讨论(0)
  • 2020-12-06 22:40

    I'm not sure what your query is intended to accomplish. I'm also not sure that parameters are allowed in the select list. MSDN used to have (many years ago, probably) a decent article on where parameters were allowed in a query, but I can't seem to find it now.

    OTTOMH, your attempts to supply the parameter values to ADO look correct. Does your query execute if you do something like this?

    SELECT 1 FROM sometable WHERE somefield = @something
    
    0 讨论(0)
  • 2020-12-06 23:00

    Here's some sample code from an MSDN Library article on preventing SQL injection attacks. I cannot find the original URL, but googling the title keywords (Preventing SQL Injections in ASP) should get you there quick enough. Hope this real-world example helps.

    strCmd = "select title, description from books where author_name = ?"
    Set objCommand.ActiveConnection = objConn
    objCommand.CommandText = strCmd
    objCommand.CommandType = adCmdText
    Set param1 = objCommand.CreateParameter ("author", adWChar, adParamInput, 50)
    param1.value = strAuthor
    objCommand.Parameters.Append param1
    Set objRS = objCommand.Execute()
    

    See the following page on MSDN, near the bottom, referring specifically to named parameters.

    MSDN example

    0 讨论(0)
  • 2020-12-06 23:02
    with server.createobject("adodb.command")
      .activeConnection = application("connection_string")
      .commandText = "update sometable set some_col=? where id=?"
      .execute , array(some_value, the_id)
    end with
    
    0 讨论(0)
提交回复
热议问题