views:

3127

answers:

8

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

A: 

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

Michael Petrotta
A: 

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.

Adam Ruth
No, it does not modify data, but it has to execute a query that is constructed dynamically so it has to be executed with sp_executesql.Also, this stored procedure calls another stored procedure that has 3 output parameters
Roxana
A: 

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

Makach
+2  A: 

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())
NinethSense
+2  A: 

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.

Remus Rusanu
A: 

Hi,

My problem is somewhat similar.. I also want to use stored procedure (which gives an output parameter ) in the selectlist of sql query. Earlier I had made UDF instead of SP but all in vain as I have to use dynamic query in UDF(which is not possible), so i have to change the approach. Thanks a lot. Any help would be appreciated...

A: 

Hi,

My problem is somewhat similar.. I also want to use stored procedure (which gives an output parameter ) in the selectlist of sql query. Earlier I had made UDF instead of SP but all in vain as I have to use dynamic query in UDF(which is not possible), so i have to change the approach. Thanks a lot. Any help would be appreciated...

A: 

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.

Niikola