简体   繁体   English

存储过程输出以查看

[英]Stored procedure output to view

I have software which can read only from tables or views. 我有只能从表或视图中读取的软件。 I need to provide this software some data from clr method (for example data from web services). 我需要为该软件提供一些来自clr方法的数据(例如,来自Web服务的数据)。 This software will read data like that: 该软件将读取以下数据:

select * from my_view WHERE somefield = 'data_identificator'

Then clr need to get that somefield as parameter and execute some other code, then return output like result from view. 然后clr需要获取该字段作为参数并执行其他代码,然后从视图返回结果(如结果)。 Is it possible at all ? 有可能吗?

This question would be much easier to answer if a full example of the intended code were provided instead of just part of it, but I think I understand enough to steer in the proper direction. 如果提供了所需代码的完整示例而不是部分示例,则此问题将更容易回答,但我想我已经足够理解,可以朝着正确的方向发展。

Yes, it is possible to get data from external sources such as Web Services, etc using SQLCLR. 是的,可以使用SQLCLR从外部资源(例如Web服务等)获取数据。 You can write a specialized scalar or table-valued functions to call specific methods and return parsed output. 您可以编写专门的标量或表值函数来调用特定方法并返回已解析的输出。 Or you can make a generic function that returns the resulting XML and then parse that in T-SQL. 或者,您可以创建一个泛型函数,该函数返回生成的XML,然后在T-SQL中对其进行解析。

If you need to do multiple steps, then you can call that SQLCLR function from a T-SQL Multistatement Table-valued Function. 如果需要执行多个步骤,则可以从T-SQL多语句表值函数调用该SQLCLR函数。 This even gives you the ability to pass in parameters. 这甚至使您能够传递参数。

Your software "which can read only from tables or views" should be able to SELECT from this Multistatement Table-valued Function (TVF) as it acts like a View that you can pass parameters into. 您的软件“只能从表或视图中读取”, 应该能够从此多SELECT表值函数(TVF)中进行SELECT ,因为它的作用类似于可以传递参数的视图。 If your software, for whatever reason, cannot select from a TVF, then you can wrap the SELECT field1, field2, ... FROM dbo.MyTVF(); 如果出于某种原因您的软件无法从TVF中进行选择,则可以包装SELECT field1, field2, ... FROM dbo.MyTVF(); in a View . 在一个View

How exactly do you write such a SQLCLR function to call a web service? 您究竟如何编写这样的SQLCLR函数来调用Web服务? Not so fast. 没那么快。 If you are asking this question in the first place, then copying-and-pasting code of this nature into a project can do more harm than good. 如果您首先问这个问题,那么将这种性质的代码复制粘贴到项目中,弊大于利。 Yes, there are several examples on various sites, possibly even here, of calling a web service in a SQLCLR function or procedure, but some (maybe even most?) are done very poorly. 是的,在各个站点(甚至在此处)都有几个在SQLCLR函数或过程中调用Web服务的示例,但是其中一些(甚至大多数?)做得很差。 Even if you are experienced in .NET programming, there are quite a few nuances to SQL Server's CLR host that you need to be aware of. 即使您有.NET编程经验,也需要注意SQL Server CLR主机的许多细微差别。 So, you really should not be writing SQLCLR code without first understanding the constraints of the environment and how to properly interact with SQL Server. 因此,在没有先了解环境的限制以及如何与SQL Server正确交互之前,您实际上不应该编写SQLCLR代码。 To assist with this, I have started writing a series on SQL Server Central: Stairway to SQLCLR (free registration required). 为了解决这个问题,我开始着手编写有关SQL Server Central的系列文章: SQLCLR的阶梯 (需要免费注册)。

I will also mention that for anyone who is interested in calling URIs but not willing or able to write any code to do it, there is a Table-Valued Function called INET_GetWebPages in the SQL# SQLCLR library that does this. 我还将提到,对于任何有兴趣调用URI但不愿意或无法编写任何代码来执行此操作的人, SQL# SQLCLR库中都有一个称为INET_GetWebPages的表值函数可以执行此操作。 Full disclosure: I am the author of SQL#, and while there is a Free version, the INET_GetWebPages function is only available in the Full version. 完全公开:我是SQL#的作者,虽然有免费版本,但INET_GetWebPages函数仅在完整版本中可用。

So, after some research and based on comments from srutzky and JamesZ final answer. 因此,经过一些研究,并根据srutzky和JamesZ的最终回答发表评论。

  1. Create CLR Table Valued Function. 创建CLR表值函数。
  2. Create view from some help table, where is stored all possible query parameter values. 从某个帮助表创建视图,在该表中存储了所有可能的查询参数值。 I don't know how to get rid of it. 我不知道如何摆脱它。 (It's not a problem for me, I have such table). (对我来说这不是问题,我有这样的桌子)。

CREATE VIEW [dbo].[MyView] AS SELECT a.*, s.ValueFromTvf FROM HelpTable a CROSS APPLY dbo.MyClrFunction(a.PropertyA) s

If I SELECT from that view: 如果我从该视图中SELECT
SELECT * FROM MyView 选择*从MyView
WHERE PropertyA = '123456' 在哪里PropertyA ='123456'
MyClrFunction will be executed with parameter '123456'. MyClrFunction将使用参数“ 123456”执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM