Re: Using results form a stored procedure in an exists clause?

From: Louis Davidson (dr_dontspamme_sql_at_hotmail.com)
Date: 04/23/04


Date: Fri, 23 Apr 2004 10:18:34 -0500

The most straightforward way is to use a temp table:
create table #temp
(
    id int
)

insert into #temp
exec sp_getIdNumbers

SELECT Persons.*
from Persons
where Perons.ID exists in (select ID from #temp)

A better way (if this is possible) is to change the sp into a table
function, then it is really simple:

SELECT Persons.*
from Persons
where Perons.ID exists in (select ID from dbo.getIdNumbers())

A couple of nitpicks, it is best not to name your procedures SP_, since that
causes additional work for the query processor (it has to check master first
to see if a procedure with that name exists.) Also, if this is permanent
code (not just testing) it is best not to use the * construct in your
queries. It is best to write out the selected column list. If you don't
need every column, it will be faster.

-- 
----------------------------------------------------------------------------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management
Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266
Note: Please reply to the newsgroups only unless you are
interested in consulting services.  All other replies will be ignored :)
"Brian Henry" <brianiupmsdn@newsgroups.nospam> wrote in message
news:OhiYZrTKEHA.1156@TK2MSFTNGP09.phx.gbl...
> How would you go about doing something like using the returned table from
> another stored procedure in a select statement to see if something exists
in
> it
>
> Say I have stored procedure
> SP_GetIDNumbers
> which returns a list of ID numbers
>
> and I want to do this
>
> SELECT Persons.* from Persons where Perons.ID exists in (stored procedure
> results)
>
> thanks, I dont have any DDL, I'm just running this one out of a question
in
> my head
>
>


Relevant Pages

  • Re: SQLserver and the WHERE x IN y
    ... to a stored procedure - that would only work if you introduced subtyping into ... the temp table from the calling procedure (the temp table is still in scope, ... INSERT INTO #MyTempTable EXECUTE YourPopulateProc 123 ... INSERT INTO @MyTableVar VALUES ...
    (microsoft.public.sqlserver.programming)
  • Re: Capture Stored PROC Output
    ... you have to create the temp table. ... exec MyProc @parm1, @parm2 ... We have a stored procedure which return a table of results ... DELETE FROM #tmp WHERE ...
    (microsoft.public.sqlserver.programming)
  • Re: Stored Procedures in SE 7.3
    ... My end goal to write a stored procedure to be used by Lotus Enterprise ... Integrator (LEI). ... into temp t1; ... from bmdata, t1 ...
    (comp.databases.informix)
  • RE: Cannot obtain recordset from stored procedure that contains Ex
    ... I am guessing the extra messages issued by the Exec ... calls were either confusing the ADO connection or actually returning multiple ... >> call a stored procedure that stuffs records into another table using a SQL ... >> temp table in the stored procedure. ...
    (microsoft.public.sqlserver.programming)
  • RE: Strange issue while executing sql server sp
    ... field is the one where truncation is occurring as my temp table field size is ... I have a simple stored procedure in sql server 2005 express version. ... ,finishdate datetime ...
    (microsoft.public.sqlserver.programming)

Loading