2013-04-11 58 views
0

存儲過程正在手動運行,但不會使用SQL Server代理運行。SQL XML解析存儲過程手動運行,未通過SQL Server代理運行

我收到以下錯誤

作爲用戶執行:XXX。 XML解析:行9,字符9,意外 輸入結束[SQLSTATE 42000](錯誤9400)。該步驟失敗。

存儲過程從Web查詢中獲取xml,將其放入臨時表中的單個單元格中。分析臨時表中的數據,並將新的易於閱讀的信息放入永久表中。我需要查詢每天運行並獲取當天的數據。

我試過查找這個問題,但我唯一的線索是XML被切碎的地方。我不知道爲什麼這隻發生在一份工作上。

你可以給的任何幫助將是偉大的。

感謝

EDIT1:我已經將過程拆分爲7層不同的步驟。這是它正在落下的一步。

INSERT XMLData(XMLD) SELECT CAST(HTML AS XML) As XMLData FROM TextData

EDIT2:下面的代碼

DECLARE 

@url varchar(2048), 
@win integer, 
@hr integer , 
@text varchar(MAX), 
@XMLdata XML, 
@Date date, 
@SearchDate nvarchar(50) 


Set @Date = GETDATE() 

set @SearchDate = CAST(@Date as nvarchar(50)) 

set @SearchDate = REPLACE(@SearchDate,'-','') 


--set @SearchDate='20130405' 

/*-- Create Temporary tables to be used to store the xml data--*/ 
/*-- We need 2 tables as the raw data from SEMO is Unicode 'UTF-8' but SQL only handles 'UTF-16'--*/ 
/*-- We must therefore store the data first as Text and then CAST it to XML datatype for easy querying later --*/ 
CREATE TABLE #TextData(HTML text NULL) 
CREATE TABLE #XMLData(XMLD xml NULL) 

/*-- This url will have to be dynamically generated each day based on GetDate() query or similiar - to be cracked out fully - random date picked for now--*/ 
Select @url = 'http://.......' 

/* Use OLE Automation Objects to go out and get the data--*/  
EXEC @hr=sp_OACreate 'WinHttp.WinHttpRequest.5.1',@win OUT 
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 

EXEC @hr=sp_OAMethod @win, 'Open',NULL,'GET',@url,'false' 
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 

EXEC @hr=sp_OAMethod @win,'Send' 
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 

INSERT #TextData(HTML) 
EXEC @hr=sp_OAGetProperty @win,'ResponseText' 
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 

EXEC @hr=sp_OADestroy @win 
IF @hr <> 0 EXEC sp_OAGetErrorInfo @win 

/*-- Now CAST the text data to XML data type which makes it much easier to query back--*/ 
INSERT #XMLData(XMLD) 
SELECT CAST(HTML AS XML) As XMLData FROM #TextData 



/*-- WE NEED TO ADD IN A SECTION HERE NOW TO PARSE THE XML DATA AND EXTRACT THE INFO WE NEED INTO A DEDICATED TABLED--*/ 

/*-- Replace the : in the #XMLData table with _ as the colons were causing errors --*/ 
UPDATE #XMLData 
SET XMLD = REPLACE(CAST(XMLD as varchar(max)),':' ,'_') 
FROM #XMLData 
WHERE CHARINDEX(':' ,CAST(XMLD as varchar(max)))>0 

UPDATE #XMLData 
SET XMLD = REPLACE(CAST(XMLD as varchar(max)),'DataSet xmlns="http_//localhost/SemoReporting/SemoAutomatedDataCollection/Datasets"' ,'DataSet') 
FROM #XMLData 
WHERE CHARINDEX('DataSet xmlns="http_//localhost/SemoReporting/SemoAutomatedDataCollection/Datasets"' ,CAST(XMLD as varchar(max)))>0 

Select @XMLdata = XMLD 
FROM #XMLData 

/*-- This is to show the colons have been replaced --*/ 
SELECT * FROM #XMLData 

Declare @i as int 
set @i = 1 

While @i < 97 
BEGIN 
/*Insert Data into SEMO Historical Table*/ 

INSERT INTO XMLParsing.dbo.T1301_SEMO_HISTORICAL_DATA 
/*-- This selects the relevant data from table1 element --*/ 
SELECT 
a.b.value('Table1[sql:variable("@i")][1]/CURRENCY_FLAG[1]','nvarchar(50)') AS [Currency], 
a.b.value('Table1[sql:variable("@i")][1]/TRADE_DATE[1]','date') AS [Trade Date], 
a.b.value('Table1[sql:variable("@i")][1]/DELIVERY_DATE[1]','date') AS [Delivery Date], 
a.b.value('Table1[sql:variable("@i")][1]/DELIVERY_HOUR[1]','int') AS [Delivery Hour], 
a.b.value('Table1[sql:variable("@i")][1]/DELIVERY_INTERVAL[1]','int') AS [Delivery Interval], 
a.b.value('Table1[sql:variable("@i")][1]/RUN_TYPE[1]','nvarchar(50)') AS [Run Type], 
a.b.value('Table1[sql:variable("@i")][1]/SMP[1]','decimal(10,4)') AS [SMP], 
a.b.value('Table1[sql:variable("@i")][1]/LAMBDA[1]','decimal(10,4)') AS [Lambda], 
a.b.value('Table1[sql:variable("@i")][1]/SYSTEM_LOAD[1]','decimal(10,4)') AS [System Load], 
a.b.value('Table1[sql:variable("@i")][1]/CMS_TIME_STAMP[1]','nvarchar(150)') AS [CMS Time Stamp] 
FROM @XMLdata.nodes('DataSet/diffgr_diffgram/EA_RESULTS') a(b) 
--where 
--not exists (Select * from SEMO_Historical_Data where [Trade Date] = @Date) 
set @[email protected]+1 
END 

Drop Table #TextData 
Drop Table #XMLData 
+0

您將不得不向我們展示一些代碼,以便我們爲您提供幫助。具體來說,我們需要看到您的作業步驟定義和您的存儲過程。 – RBarryYoung 2013-04-11 13:50:41

+0

如果您在運行時發生了某種情況,但在您安排時卻不行,則幾乎總是有權限或環境。計劃作業默認作爲SQL Server代理服務帳戶運行,並且該帳戶可能無法訪問您的帳戶所具有的所有系統資源。或者計劃作業的服務器沒有與測試服務器相同的驅動程序或配置。但是如果沒有更多關於程序的確切信息,很難說真正的問題是什麼。 – Pondlife 2013-04-11 14:21:59

+0

@JenniferNolan發佈整個過程(或任何可以重現問題的子集)。問題在代碼的早期引起。同時發佈Job Step的SQL命令文本。 – RBarryYoung 2013-04-11 14:35:44

回答

0

只是一個更新。這是不正常工作的原因是從網站採取的XML被截斷。我從來沒有完全弄清楚爲什麼會發生這種情況,但是我用wget代替了WinHttp.WinHttpRequest.5.1,它使用了網站上的所有數據,並直接將它傳遞給我可以解析數據的網頁。

相關問題