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
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
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.
function and not procedureDBO. FUNCTION_NAMEAs 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.
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:
SELECT ... WHERE EXEC someproc...