views:

120

answers:

1

I am trying to write what should ostensibly, be relative easy SQL queries - yet, I cant seeem to get them to work.

Is it possible to write a query that looks something like this:

select t.name 
  from (select * 
          from mystoredproc(1,2,3) as t) 
 where t.name = 'foobar'

Two problems with the above query:

1) First off all, the statement above dosent work. My db engine (mySQL) complains:

ERROR 1054 (42S22): Unknown column 't.name' in 'field list'

2) I want to use the returned table t in a SELF JOIN. However, I dont want to have to call mystoredproc(...) again, because it is a VERY expensive call.

Any one knows how to fix these problems?

BTW, even though I am using mySQL (for now), I would prefer if any proffered SQL snippet was db agnostic (i.e. ANSI SQL)

+12  A: 

Replace the ) as t) with ) t, as in

select t.name from (select * from mystoredproc(1,2,3)) t where t.name = 'foobar'

For your second problem: Feed the result from mystoredproc into a temporary table and SELECT from it, then you can make a self join without hassles.

Cassy
Nice... potential problem though, in that this is for a web backend and I need to differentiate between succesive calls. I am thinking of autogenerating a key for each set of records, but I could be overcomplicating things - any ideas here ..?
Stick it to THE MAN
Temporary tables are only visible to the database connection during which it was created. So even if you have several concurrent calls, all of which `CREATE TEMPORARY TABLE myTempTable`, every connection only has access to its own set of data in that table. You might need to use transactions though, otherwise the table might vanish in the middle of your queries.
Cassy
Check also with the manual: http://dev.mysql.com/doc/refman/5.0/en/create-table.html
Cassy
Sweet.... Thanks
Stick it to THE MAN