0

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 ?

7
  • yes you can pass parameter instead of 'data_identificator' Commented Jul 25, 2015 at 10:11
  • And how? Please point me how can i do that? Commented Jul 25, 2015 at 10:15
  • 1
    It's not possible to do that in a view but are you able to call a function? Commented Jul 25, 2015 at 10:33
  • Or if this somefield / 'data_identifiation' exists in a table, then you could maybe build a view on top of the function and join the data from there Commented Jul 25, 2015 at 10:35
  • @JamesZ: My software (in reality, it is not "my", it is some third party software, which I can't change) can read data from Views and Tables. I need to pass some data from external datasources: some web services. So, I can't execute any functions or procedures (only if they mimic as view or table). Idea with join is interesting - maybe it will work. Commented Jul 26, 2015 at 17:32

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, I created TVF. But then i can't pass parameters select * from TVF() WHERE field1 = 'somedata'. Or it is possible?
It would be simple, if inner CLR would return big data set, but what if i need provide some parameter to it from SELECT clause?
@VikciaR Yes, the CLR TVF can return a full result set. To pass values into a TVF from a SELECT you can use either CROSS APPLY (works like INNER JOIN: no results from TVF exclude rows from the SELECT) or OUTER APPLY (works like OUTER JOIN: you get the rows from the SELECT even if nothing is returned from the TVF). HOWEVER, be careful not to expect WHERE clause to prevent the TVF from being called. If the main SELECT returns many rows that get filtered, you should first save those results to a table variable and then SELECT from the table variable with CROSS APPLY to the CLR TVF.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.