Roy Harvey
3/8/2007 8:12:00 PM
One question is whether somelongasswhereclause returns
somelongassresultset, or notsobigresultset. If it is the later you
could save it in a temp table and then use the temp table for the IN
clause subqueries.
However if it does return somelongassresultset the lookups into the
temp table might easily be too costly. In that case you might as well
just keep doing what you posted. I would suggest considering creating
a view from that first query and then using the view in the subsequent
subqueries if only so that there is only one place where that
complicated WHERE clause is written.
Roy Harvey
Beacon Falls, CT
On Thu, 8 Mar 2007 13:52:45 -0600, "0to60"
<holeshot60_nospam@yahoo.com> wrote:
>I have a sproc that will return several tables, and it looks something like
>this:
>
>SELECT * FROM TABLEA WHERE somelongasswhereclause
>
>SELECT * FROM TABLEB WHERE TableBID IN (SELECT foreignKeyToTableB FROM
>TABLEA WHERE somelongasswhereclause)
>
>SELECT * FROM TABLEC WHERE TableBID IN (SELECT foreignKeyToTableC FROM
>TABLEA WHERE somelongasswhereclause)
>
>
>So what I'm trying to do here is get a buncha records from TableA, and then
>get a buncha records from TableB that relate to TableA and so on. I could
>just do a JOIN, but I really would like the data to be in separate tables.
>My problem is my somelongasswhereclause. Its a pretty long WHERE clause
>with lots of conditions, and it seems to me that having to evaluate them
>over and over is a waste. What options do I have here? I was thinking of
>opening a cursor on TABLEA and creating an IN clause of the TABLEA IDs, so
>that in the following queries I would never have to say "...FROM TABLEA
>WHERE somelongasswhereclause" again. I could just say "...FROM TABLEA WHERE
>tableAID IN (...)", but I still need to return TABLEA and how would I do
>that if I made a cursor out of it?