0

Possible Duplicate:
SELECT FROM stored procedure?

How should I include a stored procedure in a T-SQL syntax?

select * from CITY_TABLE C where 
    COUNT_PEOPLE_PROCEDURE(C.ID) > 3
4
  • @Diego you can't say SELECT ... WHERE EXEC someproc... Commented Nov 22, 2012 at 17:28
  • @Diego OK, can you clean up your guesswork comments now so future readers don't think you're serious? Commented Nov 22, 2012 at 17:29
  • What duplicate? there is a complicated question, mine is much simpler, and there is no answer. Commented Nov 22, 2012 at 17:30
  • This is not asking about using a procedure as a data source; it is asking about using a procedure in a WHERE clause, which is conceptually quite different. This is not a duplicate of SO 3730718. Commented Nov 22, 2012 at 20:17

3 Answers 3

3

You cannot include stored procedure into query like this.

Instead, you can make scalar function

create function COUNT_PEOPLE_FUNCTION
(
    @ID int
)
returns int
as
begin
    declare @Result int

    <... your code here ...>

    return @Result
end

and then

select * from CITY_TABLE C where COUNT_PEOPLE_FUNCTION(C.ID) > 3

But, actually, it may slow your query, because scalar function will be called for each row independently.

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

6 Comments

I don't see any difference in select with my variant...
the only difference is that you have to create function and not procedure
@serhio look closer... that's a function not a procedure.
ok, bold function is better )
To remark, there is a need to specify DBO. FUNCTION_NAME
|
1

As per your comment, if you need to get the only records which have n occurrencies, you could use a subquery like this:

SELECT * FROM CITY_TABLE WHERE IDPerson IN (
    SELECT IDPerson FROM CITY_TABLE GROUP BY IDPerson HAVING COUNT(*) > 3
)

Obviously i cannot know the exact column name, but this is the idea.

5 Comments

thank you, but I ask about stored procedures, not about a way to bypass it...
IMHO you can't use SP that way... you should use a function as stated above, with a big performance hit on large data sets.
I have a complicated select with multiple JOINs and I use this in multiple places in the sql... So I need to store it somewhere to use.
Ok, but your SP needs a parameter that is unique for each row of your initial select, is it correct? If so, you need an inline function, or a subquery.
However, playing with temporary tables & subqueries you could get a big performance improvement if compared to a scalar function.
0

As others already said you cannot use a stored procedure like that, i suggest you to use a function if you can; If you can't use a function (and even if you can) i suggest you this page as a useful reading:

How to Share Data between Stored Procedures

2 Comments

fortunately, I don't need to share data between SP, but thank you anyway for the article.
actually it could be useful, it says "How can I use the result set from a stored procedure in a SELECT statement?", by the way, as everybody says, you can't use SPs like the way you are using in your example

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.