2011-03-20 61 views
2

我試圖在ASP測試程序中針對MS SQL 2008數據庫運行以下查詢。查詢在'Server Management Studio'中工作得很好,我可以從ASP程序連接到數據庫,沒有任何問題。然而,當我把它放在一起,我得到以下錯誤:SQL查詢可以在管理工作室中正常工作,但不能從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)) 

有什麼明顯錯誤?另外,是否有一個很好的方法來調試這種性質的問題?謝謝!

+0

顯然錯誤的是你使用動態TSQL而不是存儲過程! – 2011-03-20 02:39:01

+0

嘗試在'BEGIN ... END'中包裝所有的SQL。 – 2011-03-20 02:45:10

+0

我不會認爲BEGIN ... END會做任何事情。你可以檢查r.ActiveConnection.Errors集合來查看是否有來自提供者的額外錯誤?您發佈的內容顯然不符合您的查詢。 – 2011-03-20 02:53:41

回答

1

當字段名稱拼錯時,通常會出現此錯誤。仔細檢查你的SQL。

如果你是絕對確定你的SQL是正確的,看看SET NOCHECK ON會有幫助。

請考慮意見中的建議,並使用存儲過程進行操作。避免自己的痛苦。

+0

'SET NOCHECK ON'似乎能夠解決問題。我想我會採納每個人的建議,並使用存儲過程。謝謝! – wkf 2011-03-21 02:19:38

相關問題