Reputation: 1
In SSMS I could create something like the temp table below. I could then run queries, joins, or additional temp table/cte creations off of #apps while only clicking the execute button ONCE.
SELECT
Visit_Type
,fullname
,appointment_date
,site_location
INTO #apps
FROM appointments
WHERE appointment_date BETWEEN '2024-07-01' AND '2024-07-31'
Now in a postgresql environment (dbeaver), I have to babysit each section of the code. Meaning; if the script contains 3 temp tables, I have to run them each independently. I've tried a variety of BEGIN and COMMIT calls but they seem to have no effect. Is there a way I can structure a script with temp tables and only have to execute the job once? Any help would be much appreciated.
For example, in dbeaver I would have to run these two sections separately. This is quite tedious for queries that have a long runtime. If I highlight both sections I get a 'syntax error at or near "CREATE"' error message. If I don't highlight, only the first section will execute.
---------------*Section 1*-----------------------
CREATE TEMP TABLE apps AS
SELECT
Visit_Type
,fullname
,appointment_date
,site_location
FROM appointments
WHERE appointment_date BETWEEN '2024-07-01' AND '2024-07-31'
;
---------------*Section 2*-----------------------
Select apps.*
,site_dep.department_name
from apps
left join site_dep
on apps.site_location = site_dep.location_name
and site_dep.state = 'CA'
;
Upvotes: 0
Views: 92
Reputation: 14934
DBeaver can run both sections of the above several ways:
SQL Editor
menu click the option Execute SQL Script
.ALT-X
.The above is the general case for executing multiple SQL statements.
In you specific case you can do this in a single statement. You either replace your temp table with a CTE or you just do a direct join of the tables.
-- convert temp table to CTE --
with apps as
( select visit_type
, fullname
, appointment_date
, site_location
from appointments
where appointment_date between '2024-07-01' and '2024-07-31'
)
select apps.*
, sd.department_name
from apps
left join site_dep sd
on apps.site_location = sd.location_name
and sd.state = 'CA';
-- direct Join of tables --
select apps.visit_type
, apps.fullname
, apps.appointment_date
, apps.site_location
, sd.department_name
from appointments apps
left join site_dep sd
on apps.site_location = sd.location_name
where apps.appointment_date between '2024-07-01' and '2024-07-31'
and sd.state = 'CA';
Upvotes: 1