之外我有處理XML數據的查詢和我使用while(@@FETCH_STATUS = 0)
循環從遊標返回的數據。@@ FETCH_STATUS住光標
當我運行使用Management Studio查詢,@@FETCH_STATUS
等於-1並且省略了我的循環內的代碼。如果我使用調試器運行查詢並按繼續,它運行得很好,並且@@FETCH_STATUS
等於0.當我再次運行查詢時,在調試中運行後@@FETCH_STATUS
等於0並更改爲-1。與調試器中運行@@FETCH_STATUS
還是等於一次後@@FETCH_STATUS = 0
(我想這個數值)
- 我SSMS運行 -
@@FETCH_STATUS = -1
- 我用調試器中運行:
綜上所述0但是然後變爲-1。
我使用OPEN cursor
,CLOSE cursor
和DEALLOCATE cursor
。它爲什麼這樣工作?
編輯:代碼你問:
IF (OBJECT_ID('dbo.XmlOrderResponses') IS NOT NULL)
DROP TABLE XmlOrderResponses;
CREATE TABLE XmlOrderResponses (
OrderResponseType INT
,OrderResponseNumber NVARCHAR(40)
,OrderResponseDate DATETIME
,DocumentFunctionCode NVARCHAR(40)
,Remarks INT
);
DECLARE CUR CURSOR
FOR
SELECT Subdirectory
FROM XMLFiles;
OPEN CUR
WHILE (@@FETCH_STATUS = 0)
BEGIN
DECLARE @DocHandle AS INT;
DECLARE @TMP AS NVARCHAR(512);
FETCH NEXT
FROM Cur
INTO @TMP
DECLARE @XmlDocument AS NVARCHAR(MAX);
SET @XmlDocument = (
SELECT CAST(XMLSource AS NVARCHAR(max))
FROM XMLFiles
WHERE subdirectory = @TMP
);
EXEC sys.sp_xml_preparedocument @DocHandle OUTPUT
,@XmlDocument;
INSERT INTO XmlOrderResponses (
OrderResponseType
,OrderResponseNumber
,OrderResponseDate
,DocumentFunctionCode
,Remarks
)
SELECT *
FROM OPENXML(@DocHandle, '/Document-OrderResponse/*', 11) WITH (
OrderResponseType INT
,OrderResponseNumber NVARCHAR(40)
,OrderResponseDate DATETIME
,DocumentFunctionCode NVARCHAR(40)
,Remarks INT
);
EXEC sys.sp_xml_removedocument @DocHandle;
END
CLOSE CUR;
DEALLOCATE CUR;
--I know I shouldn't be doing that but I can't get rid of NULL records the other way.
DELETE
FROM XmlOrderResponses
WHERE OrderResponseType IS NULL
AND OrderResponseNumber IS NULL
AND OrderResponseDate IS NULL
AND DocumentFunctionCode IS NULL
AND Remarks IS NULL;
SELECT *
FROM XmlOrderResponses
SELECT @@FETCH_STATUS
我想我們需要看到更多的代碼來理解你所要求的。你可以發佈SQL嗎? – DavidG 2014-10-17 10:02:25
記住'@@ FETCH_STATUS'對於連接上的所有遊標是全局的,所以即使當前正在「查看」的遊標沒有改變狀態,在相同連接上運行的其他遊標也可以更改@@ FETCH_STATUS。由於使用斷點執行的延長性質,這在調試時可能尤其成問題。你可能希望使用''SELECT [fetch_status] FROM sys.dm_exec_cursors(@@ SPID)WHERE Name ='YourCursorName''而不是'@@ FETCH_STATUS'。 – GarethD 2014-10-17 10:16:52
當你發佈你的代碼時,我們甚至可以檢查你是否真的需要光標? :) – NickyvV 2014-10-17 10:25:30