您的位置:首页 > 数据库

sql servr 操作xml&获取最新的自增主键

2015-08-07 09:10 351 查看
declare @ServiceUrl as varchar(1000) 

declare @UrlAddress varchar(500)  

--WebService地址:以http开头,结尾带斜杠,例如'http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/' 

set @UrlAddress = 'http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/' 

--set @UrlAddress = 'http://172.161.251:8811/Service.asmx/'

declare @FunName varchar(50)

--WebService中调用的方法名:例如'getMobileCodeInfo'

set @FunName = 'getMobileCodeInfo'  

--set @FunName = 'HelloWorld'       ---172.16.1.251

--以下参数对应WebService中4个参数的[参数名]

declare @P1 varchar(800),@P2 varchar(100)

set @P1 = 'mobileCode'

set @P2 = 'userid' 

declare @P1_Value varchar(100),@P2_Value varchar(100)

set @P1_Value = '13800138000'

set @P2_Value = '' 

set @ServiceUrl = @UrlAddress + @FunName + '?' + @P1 + '=' + @P1_Value +'&' + @P2 + '=' + @P2_Value                                           

--set @ServiceUrl = @UrlAddress + @FunName + '?'---测试HelloWord方法

Declare @Object as Int

Declare @ResponseText as Varchar(8000)                    

Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;

Exec sp_OAMethod @Object, 'open', NULL, 'get',@ServiceUrl,'false'

Exec sp_OAMethod @Object, 'send'

Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT      

Select @ResponseText     --返回xml字符串

declare @xmlDoc xml

Select @xmlDoc=CAST(@ResponseText as xml)

select @xmlDoc

 select @xmlDoc.value('(/string)[1]', 'nvarchar(max)')

Exec sp_OADestroy @Object

 

如果出现异常 : 因 URL 意外地以“/HelloWorld”结束,请求格式无法识别。

因 URL 意外地以“/HelloWorld”结束,请求格式无法识别。

在web.config文件中的 <system.web> 节点下加入:

<webServices>

    <protocols>

        <add name= "HttpPost"/>

        <add name= "HttpGet"/>

    </protocols>

</webServices>

 

---读取xml

declare @xmlDoc xml

set @xmlDoc='<string id="0001"> 258

</string>'

select @xmlDoc

select @xmlDoc.value('(/string)[1]', 'nvarchar(max)')

 --当前最新的自增主键

select ident_current('表名') --当前最新的自增主键
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: