将SQL Server 2014 JSON数据POST到Microsoft BI API

时间:2016-10-05 22:10:32

标签: json sql-server-2014 powerbi

我正在寻找POST JSON数据到Microsoft BI API的解决方案。

或者最好创建一个将数据POST到BI而不是每日SQL代理的Windows Service应用程序?

我找到了解决方案:

EXEC sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'Ole Automation Procedures', 1
RECONFIGURE

CREATE PROCEDURE [dbo].[HTTP_POST]
@parameters NVARCHAR(max), @response NVARCHAR(max) OUTPUT
WITH EXEC AS CALLER
AS
Declare @obj int,@hr int,@status int,@msg varchar(255),@sUrl varchar(2000)
set @sUrl = 'https://api.powerbi.com/'
exec @hr = sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT

if @hr <> 0 begin Raiserror('sp_OACreate MSXML2.ServerXMLHttp.3.0
failed', 16,1) return end
exec @hr = sp_OAMethod @obj, 'open', NULL, 'POST', @sUrl, false
if @hr <>0 begin set @msg = 'sp_OAMethod Open failed' goto eh end
exec @hr = sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Type', 'application/json'
if @hr <>0 begin set @msg = 'sp_OAMethod setRequestHeader failed' goto
eh end
exec @hr = sp_OAMethod @obj, send, NULL, @parameters
if @hr <>0 begin set @msg = 'sp_OAMethod Send failed' goto eh end
exec @hr = sp_OAGetProperty @obj, 'status', @status OUT
if @hr <>0 begin set @msg = 'sp_OAMethod read status failed' goto
eh
end
if @status <> 200 begin set @msg = 'sp_OAMethod http status ' + str(@status) goto eh end
--exec @hr = sp_OAGetProperty @obj, 'responseText', @response OUT
--if @hr <>0 begin set @msg = 'sp_OAMethod read response failed' goto
--eh end
exec @hr = sp_OADestroy @obj
return
eh:
exec @hr = sp_OADestroy @obj
    Raiserror(@msg, 16, 1)
return
GO

0 个答案:

没有答案
相关问题