Re: Multiple queries into a single temp table
Posted in 1998
Topics: SQL Development & Query Writing, Data Types & Schema Design
Steve Dussinger wrote:
> I have several queries I wish to do, and I want to place the results
> of all of these different queries into
> 1 temp table, so I can return that to my program.
>
> I tried the following:
>
> select 1 col1, dealer_id col2 from dealer into temp
> dlr_manu_tbl;
> select 2 col1, manufacturer_id col2 from manufacturer into temp
> dlr_manu_tbl;
>
> This failed, telling me that the table dlr_manu_tbl exists when the
> second select runs.
Your first select statement creates a temporary table named
dlr_manu_tbl, and your second query tries to create another table with
the same name, so the error is expected.
Here are a couple solutions for you:
1) Use the UNION operator:
select id as col1, dealer as col2 from dealer
UNION -- (or UNION ALL)
select id as col1, manufacturer as col2 from manufacturer
into temp dlr_manu_tbl;
2) Create the temporary table as a seperate step:
create TEMP table dlr_manu_tbl (col1 int, col2 varchar(40));
insert into dlr_manu_tbl
select id as col1, dealer as col2 from dealer;
insert into dlr_manu_tbl
select id as col1, manufacturer as col2 from manufacturer;
I have no idea what the practical value of this new table is, but
hopefully this'll get you going...
Cheers,
Jay