SQL Query在管理工作室中运行良好,但在ASP中运行不正常

时间:2011-03-20 02:35:43

标签: sql asp-classic ado

我正在尝试对ASP测试程序中的MS SQL 2008数据库运行以下查询。查询在“Server Management Studio”中工作得很好,我可以毫无问题地从ASP程序连接到数据库。但是,当我把它们放在一起时,我收到以下错误:

ADODB.Recordset error '800a0cc1'

Item cannot be found in the collection corresponding to the requested name or ordinal.

/test.asp, line 59

第59行符合以下代码段中的最后一行:

set c = Server.CreateObject("ADODB.connection")
c.Open "PROVIDER=sqloledb;SERVER=DESKTOP-VM-WIN7\SQLEXPRESS;DATABASE=ExamDB;UID=USERNAME;PWD=PASSWORD;"
set r = c.Execute("DECLARE @session_id uniqueidentifier; " &_
              "SET @session_id = 'BFAABD2A-4717-4619-8CF6-E20009B97E8F'; " &_
              "DECLARE @exam_id uniqueidentifier; " &_
              "SET @exam_id = (SELECT exam_id " &_
              "                FROM sessions " &_
              "                WHERE session_id = @session_id); " &_
              "CREATE TABLE #exam (concept_title nvarchar(50), " &_
              "                    concept_description xml, " &_
              "                    concept_questions xml); " &_
              "DECLARE @concept_id uniqueidentifier; " &_
              "DECLARE concept_cursor CURSOR FOR " &_
              "SELECT concept_id " &_
              "FROM exams " &_
              "WHERE exam_id = @exam_id; " &_
              "OPEN concept_cursor; " &_
              "FETCH NEXT FROM concept_cursor INTO @concept_id; " &_
              "WHILE @@FETCH_STATUS = 0 " &_
              "BEGIN " &_
              "    INSERT #exam " &_
              "    SELECT concepts.concept_title, " &_
              "           concepts.concept_description, " &_
              "           (SELECT questions.question_title, " &_
              "                   questions.question_description, " &_
              "                   questions.question_answer, " &_
              "                   questions.question_difficulty " &_
              "            FROM questions " &_
              "            INNER JOIN concepts " &_
              "            ON questions.question_id = concepts.question_id AND concepts.concept_id = @concept_id " &_
              "            ORDER BY concepts.question_order " &_
              "            FOR XML PATH('question'), ELEMENTS, TYPE) " &_
              "    FROM concepts " &_
              "    WHERE concepts.concept_id = @concept_id AND concept_title IS NOT NULL; " &_
              "    FETCH NEXT FROM concept_cursor INTO @concept_id; " &_
              "END; " &_
              "SELECT * FROM #exam " &_
              "FOR XML PATH('concept'), ELEMENTS, ROOT('exam'), TYPE; " &_
              "CLOSE concept_cursor; " &_
              "DEALLOCATE concept_cursor; " &_
              "DROP TABLE #exam; ")
Response.Write(r(0))

有什么明显的错误吗?此外,有没有一种很好的方法来调试这种性质的问题?谢谢!

1 个答案:

答案 0 :(得分:1)

当字段名称拼写错误时,通常会出现此错误。仔细检查你的SQL。

如果您绝对确定您的SQL是正确的,请查看SET NOCHECK ON是否有帮助。

请考虑评论中的建议,并使用存储过程进行操作。避免自己充满痛苦。