Question

I need to make a SELECT with a call of a stored procedure in the WHERE clause. It should be something like that....

SELECT distinct top 10 i.x, d.droit FROM v_droit d, v_info i WHERE d.nomdroit='yy' AND i.id<>2 AND (select val from (exec up_droits(i.x, d.droit)) <>3

But it does not work... Any idea?

Don't say to replace the stored procedure with a function because is not possible to use the existing code in a function. So the function is not a valid option. I really need to be able to use a stored procedure

Was it helpful?

Solution

This is achieved by first executing the stored procedure, capturing the output into a #temp table or a @tabel variable, then running your query against the table. Something like this:

declare @droits_table (val ,... );
insert into @droits_table
exec up_droits(param, param);
SELECT distinct top 10 i.x, d.droit FROM v_droit d, v_info i WHERE d.nomdroit='yy' AND i.id<>2 AND (select val from @droits) <>3

Of course this will not work for you because the up_droits needs the i.x and d.droit parameters from the query. This indicates that your stored procedure should probably be a a view or table valued function.

OTHER TIPS

Sorry but, make it a table valued function rather than stored procedure.

Eg:

Scalar - SELECT id, name FROM test WHERE id < (SELECT dbo.mytestfunction())
Table - SELECT id, name FROM test WHERE id = (SELECT col1 from dbo.mytestfunction())

You can't. The content of the WHERE clause must be a search expression.

Is the reason that the code doesn't work as a function because it modifies some data? If so, then you're out of luck, functions used in where clauses must be immutable.

If the stored procedure doesn't modify any data, you may be able to wrap it inside of a function.

If you are on SQL Server I don't think you can do what you propose.

But one thing you can do is build dynamic queries, but be careful doing it because they open up many interesting problemareas.

The syntax is :

EXEC @<query>

But anotherthing you can do, which is probably much better for you, is to make the up_droits function deliver it's results in a temp table, if you select into a #table it is temporary for the duration of your function/procedure scope


declare procedure up_droits() as
select val .. into #temp

So what you do is create a procedure


create procedure Top10FromDroit
begin
  exec up_droits
  SELECT distinct top 10 i.x, d.droit FROM v_droit d, v_info i WHERE d.nomdroit='yy' AND i.id2 AND (select val from (#temp) 3

Hopefully that will give you the results you want to achieve.

If at first you don't succeed, code around it^^

Could anyone of you explain reasons for executing dynamic SQl inside stored procedure. I know very few situations when you need them - but really very few. 99.9% (or 999 of a 1000) of execute strings could be rewritten as normal sql statements with parameters.

The very same is with Selects that have functions inside select or where clauses.

Try to think about your sets of data, not about procedural ways how to solve it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top