什么?SQL Server也是编程语言。能够发起HTTP通信。
第一步 开启sqlServer通信组件
sp_configure 'show advanced options', 1; GO RECONFIGURE; GO sp_configure 'Ole Automation Procedures', 1; GO RECONFIGURE; GO
第二步 查看组件状态 (不报错就说明开启成功了)
EXEC sp_configure 'Ole Automation Procedures'; GO
第三步 http请求方法
CREATE PROCEDURE http_tool ( @httpType VARCHAR(8000) , @contentType VARCHAR(8000) , @authHeader VARCHAR(8000) , @url VARCHAR(8000) , @data VARCHAR(8000) , @json VARCHAR(8000) OUTPUT ) AS BEGIN DECLARE @Status INT; DECLARE @responseText VARCHAR(8000); EXEC sp_OACreate 'MSXML2.ServerXMLHTTP', @Status OUT; EXEC sp_OAMethod @Status, 'Open', NULL, @httpType, @url, 'false'; EXEC sp_OAMethod @Status, 'setRequestHeader', NULL, 'Authorization', @authHeader; EXEC sp_OAMethod @Status, 'setRequestHeader', NULL, 'Content-type', @contentType; EXEC sp_OAMethod @Status, 'send', NULL, @data; EXEC sp_OAMethod @Status, 'responseText', @responseText OUTPUT; SET @json = @responseText; END; GO
第四步 简单的json解析器
CREATE PROCEDURE json_tool ( @json VARCHAR(8000) , @key VARCHAR(8000) , @value VARCHAR(8000) OUTPUT ) AS BEGIN DECLARE @keyLenght INT = LEN(@key); DECLARE @keyIndex INT= CHARINDEX(@key, @json, 0); SET @keyIndex = @keyIndex + @keyLenght + 3; DECLARE @jsonLenght INT= LEN(@json); --掐头 SET @json = SUBSTRING(@json, @keyIndex, @jsonLenght); DECLARE @valueEnd INT = CHARINDEX('"', @json, 0); --去尾 SET @value = SUBSTRING(@json, 0, @valueEnd); END GO
第五步 测试
/*获取 含有 token 的 json*/ --获取token的地址 DECLARE @url_get_token VARCHAR(500)= 'https://XXXXXX/api-uaa/oauth/user/token'; DECLARE @token_json VARCHAR(8000); -- EXEC http_tool 'POST', 'application/x-www-form-urlencoded', 'Basic Xxxxxxxxxxxxxxxxxxxx', @url_get_token, 'username=sa&password=123', @token_json OUTPUT; /*解析 得到 token*/ DECLARE @token VARCHAR(800); -- EXEC json_tool @token_json, 'access_token', @token OUTPUT;标签:VARCHAR,EXEC,token,Server,json,SQL,HTTP,8000,DECLARE From: https://www.cnblogs.com/luyj00436/p/16719119.html