简体   繁体   中英

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). 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. 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. 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.

If you need to do multiple steps, then you can call that SQLCLR function from a T-SQL Multistatement Table-valued Function. 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. If your software, for whatever reason, cannot select from a TVF, then you can wrap the SELECT field1, field2, ... FROM dbo.MyTVF(); in a View .

How exactly do you write such a SQLCLR function to call a web service? 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. 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. 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. To assist with this, I have started writing a series on SQL Server Central: Stairway to SQLCLR (free registration required).

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. 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.

So, after some research and based on comments from srutzky and JamesZ final answer.

  1. Create CLR Table Valued Function.
  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 * FROM MyView
WHERE PropertyA = '123456'
MyClrFunction will be executed with parameter '123456'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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