怎样行使T-SQL从URL读取XML?
发布时间:2021-03-02 13:38:11 所属栏目:编程 来源:网络整理
导读:在url中有xml文件: responsesum0/sumresult0/resultcommentsel*1.9488|buy*1.9453/comment/response 此刻想要存储进程,我可以从url理会这个xml文件,并更新为 comment中的列值.ll * 1.9488 | buy * 1.9453 / comment想添加购置* 1.9453到我的桌子.怎么样? 解
在url中有xml文件: <response> <sum>0</sum> <result>0</result> <comment>sel*1.9488|buy*1.9453</comment> </response> 此刻想要存储进程,我可以从url理会这个xml文件,并更新为< comment>中的列值.ll * 1.9488 | buy * 1.9453< / comment>想添加购置* 1.9453到我的桌子.怎么样? 办理要领要从URL获取XML,您必要执行以下操纵:启用Ole Automation Procedures sp_configure 'show advanced options',1; GO RECONFIGURE; GO sp_configure 'Ole Automation Procedures',1; GO RECONFIGURE; GO 然后,为了从URL获取XML(基于here的更新版本答复),下面建设一个姑且表来存储该值,以便您可以行使xpath和子字符串处理赏罚功效. 这是一个行使google maps xml的事变示例,您必要更新url和xpath以满意您的特定要求. USE tempdb GO IF OBJECT_ID('tempdb..#xml') IS NOT NULL DROP TABLE #xml CREATE TABLE #xml ( yourXML XML ) GO DECLARE @URL VARCHAR(8000) DECLARE @QS varchar(50) -- & or ? depending if there are other query strings -- Use this for when there is other query strings: SELECT @QS = '&date='+convert(varchar(25),getdate(),126) -- Use this for when there is NO other query strings: -- SELECT @QS = '?date='+convert(varchar(25),126) SELECT @URL = 'http://maps.google.com/maps/api/geocode/xml?latlng=10.247087,-65.598409&sensor=false' + @QS DECLARE @Response varchar(8000) DECLARE @XML xml DECLARE @Obj int DECLARE @Result int DECLARE @HTTPStatus int DECLARE @ErrorMsg varchar(MAX) EXEC @Result = sp_OACreate 'MSXML2.XMLHttp',@Obj OUT EXEC @Result = sp_OAMethod @Obj,'open',NULL,'GET',@URL,false EXEC @Result = sp_OAMethod @Obj,'setRequestHeader','Content-Type','application/x-www-form-urlencoded' EXEC @Result = sp_OAMethod @Obj,send,'' EXEC @Result = sp_OAGetProperty @Obj,'status',@HTTPStatus OUT INSERT #xml ( yourXML ) EXEC @Result = sp_OAGetProperty @Obj,'responseXML.xml'--,@Response OUT SELECT yourXML.value('(//GeocodeResponse/status)[1]','VARCHAR(MAX)') from #xml 为了插入子字符串,您必要执行相同这样的操纵以返回管道后的全部内容并添加到表中: INSERT tableDestination (valueDestination) SELECT substring(yourXML.value('(//response/comment)[1]','VARCHAR(MAX)'),charindex('|',yourXML.value('(//response/comment)[1]',1)+1,len(yourXML.value('(//response/comment)','VARCHAR(MAX)'))) from #xml (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐
热点阅读