1

我正在尝试使用变量作为 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)
%>

4

1 回答 1

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
%>
于 2013-01-09T04:55:27.223 回答