HTTP 请求 - HTML Table Import/Select 进入 SQL 服务器 Table CLR 或 TSQL

HTTP Request - HTML Table Import/Select Into SQL Server Table CLR or TSQL

背景: 首先,我认为重要的是要注意这是开发机器上的 SQL Express Server 运行ning。该服务器仅用作各种 excel 报告的提要。

由于操作孤岛,我无法直接访问存储我需要的数据的所有数据库。我目前正在做的是使用 Excel VBA 查询 table 下载 SSRS table,导入访问,然后使用 table 获取访问权限链接服务器。

问题:

我稍微修改了这个CLR代码以通过SSRS授权。我只是不确定 how/if 我可以 return 以一种让我导入 SQL 服务器上的 table 的方式发送 http 请求。

然后我将设置一个 windows 任务以每天自动 运行 sql/grab 数据,从进程中删除 vba/access 数据库。

如果您对替代方法有任何提示或建议,我不太关心我是如何到达那里的。感谢您的帮助!

Imports System.IO
Imports System.Data.SqlTypes
Imports System.Net
Public Class FavoriteCLRs
<Microsoft.SqlServer.Server.SqlFunction()>
Public Shared Function CLR_WebQuery(ByVal URL As String) As String
    Dim cookieContainer As CookieContainer = New CookieContainer()

    Dim myCredentials As New NetworkCredential("user", "password")
    ' Create a WebRequest with the specified URL. 
    Dim request As HttpWebRequest = CType(HttpWebRequest.Create(URL), HttpWebRequest)
    myCredentials.Domain = "domain"
    request.Credentials = myCredentials

    request.Timeout = 10000
    request.Method = "GET"
    request.KeepAlive = True
    request.AllowAutoRedirect = True
    request.PreAuthenticate = True
    request.CookieContainer = cookieContainer
    Using response As WebResponse = request.GetResponse()
        Using dataStream As Stream = response.GetResponseStream()
            Using reader As New StreamReader(dataStream)
                Dim responseFromServer As String = reader.ReadToEnd()
                Return responseFromServer
            End Using
        End Using
    End Using
    request = Nothing
End Function
End Class

从 SSRS 返回的数据是什么格式?是XML吗?在不了解确切格式的情况下,很难给出详细的建议,但我可以提一些需要考虑的事情:

  1. 如果要保留标量函数,需要用T-SQL解析SSRS输出。如果来自 SSRS 的数据是 XML 那么您可以通过 .nodes() 函数轻松地做到这一点。

  2. 无论返回的数据是 XML 还是带分隔符的,甚至是 HTML table,文本解析通常都会由 .NET 更有效地处理。因此,在 request = Nothing 行之后不返回 responseFromServer,而是将输出解析为行并将函数更改为 Table-Valued Function (TVF),以便您可以传回一个table。然后你可以很容易地在 SQL 服务器中填充 table 使用:

    INSERT INTO dbo.SomeTable (Col1, Col2, ...)
      SELECT Field1, Field2,...
      FROM   dbo.CLR_WebQuery(N'http://some/URL.aspx?stuff');