无效的字符错误(ADO / ASP)

时间:2013-01-09 03:52:57

标签: sql asp-classic ado

我正在尝试将变量用作SQL中的列字段。但是,当我运行代码时,我收到以下错误:


Microsoft VBScript compilation error '800a0408'

Invalid character

/junk/dbresults.htm, line 25

DECLARE @cat char(20)
--------^

代码如下:

<%
    Dim connectString, connect, conDB, con
    connectString = "Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & Server.MapPath("data")
          src_abn = Request.QueryString("abn")
          src_cat = Request.QueryString("cat")
    set connect = Server.CreateObject("ADODB.connection")
    connect.open connectString

    DECLARE @cat char(20)
    DECLARE @dynamicsql char(1000)

    SET @cat = src_cat;
    SET @dynamicsql = "SELECT * FROM cont.csv WHERE @cat='Yes'"

    if src_abn = "all" then
    conDB = EXEC (@DynamicSQL)  
    else
    conDB = "SELECT * FROM cont.csv WHERE ucase(abn) LIKE ucase('%"+src_abn+"%')"   
    end if

    set con = connect.execute(conDB)
%>

1 个答案:

答案 0 :(得分:2)

我不肯定你正在尝试做什么,但你不能用经典的ASP运行SQL语法(即DECLARE @cat ...)。你想做这样的事吗?

<%
    //Request your variables
    src_abn = Request.QueryString("abn")
    src_cat = Request.QueryString("cat")

    Dim connectionString, dbConn, rsResults, strSQL

    //Setup your connection
    connectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=" & Server.MapPath("data")
    set dbConn= Server.CreateObject("ADODB.Connection")
    dbConn.open connectionString

    //Create your sql statement
    if src_abn = "all" then
        strSQL = "SELECT * FROM cont.csv WHERE " & src_cat & "='Yes'"
    else
        strSQL = "SELECT * FROM cont.csv WHERE ucase(abn) LIKE ucase('%" & src_abn & "%')"   
    end if

    //Create your results
    set rsResults = Server.CreateObject("ADODB.Recordset")
    rsResults.open strSQL, dbConn

    //Loop through your results
    While Not rsResults.EOF

        rsResults.MoveNext
    Wend

    //Closes results and connection
    rsResults.Close
    dbConn.Close
%>