sql_language thread: Stored Procedure Question...
The approach you proposed will work just fine.
Another approach is to use the UNION operator.
CREATE PROCEDURE sp_mypro
AS
BEGIN /*Procedure*/
SELECT fld1, fld2, fld3 FROM table1
UNION
SELECT blah, blah2, blah3 FROM Table2
UNION
SELECT junk, junk2, NULL FROM Table3
END /*Procedure*/
The "recordset" that will be returned from calling the stored procedure will
be the combination of all three SELECT statements. If you want to ORDER
them, do that on the LAST select statement.
Notes:
The NULL for the 3rd field on the last Table is a "place holder". Since
there are only 2 fields of interest in the last query, and UNION requires
the same number of fields and same datatypes for each corresponding field
between the queries, you have to "fill" that column with something. It
could also be a scalar value like a number, or whatever as long as it
"matches" the datatypes of the first SELECT statement. Also note, that the
FIRST select statement is what determines the datatypes that the other two
select statements much mach.
Hope this helps
-----Original Message-----
From: James Q. Stansfield [mailto:jstansfield@t...]
Sent: Thursday, March 22, 2001 4:05 PM
To: sql language
Subject: [sql_language] Stored Procedure Question...
I've writen a tidy little stored procedure that grabs a bunch of data from
across a few tables, performs calculations against it and then sned back a
nice recordset with the results.
My problem is that this should be happening a few times with different sets
of data and then return the 'sets' of records set ordered. My first thought
should be to create a loop that calls my SP and inserts this data into a
temporary table. I can then sort and return this temporary table to the
calling process.
Any thoughts or ideas?