Select into temp table in PostgreSQL? [duplicate] - sql

This question already has answers here:
Creating temporary tables in SQL
(2 answers)
Closed 7 years ago.
How to create temporary table using select into in PostgreSQL. For example in SQL Select * into temp_tab from source_tab;

You can try to use Create Table As command like this:
CREATE TEMP TABLE mytable AS
SELECT * from source_tab;
From the docs:
This command is functionally similar to SELECT INTO, but it is preferred since it is less likely to be confused with other uses of
the SELECT INTO syntax. Furthermore, CREATE TABLE AS offers a superset
of the functionality offered by SELECT INTO.
The CREATE TABLE AS command allows the user to explicitly specify
whether OIDs should be included. If the presence of OIDs is not
explicitly specified, the default_with_oids configuration variable is
used.

Related

Why does Access 2016 reject the syntax [duplicate]

This question already has an answer here:
MS Access: Syntax error in CREATE TABLE statement
(1 answer)
Closed 1 year ago.
I want to create a table with selected columns from an existing larger table The syntax below is what is recommended but ACCESS throws an error msg. What gives?
CREATE TABLE new_table_name AS
SELECT column1, column2
FROM existing_table_name
WHERE ....;
Every brand of SQL database can choose how complete its implementation of the SQL language is. MS Access in particular fails to support a lot of language features that are common in other brands of database.
You just have to verify the syntax you want to use is supported. You can do this by referring to the syntax documentation, for example: CREATE TABLE Statement. The syntax and description makes no mention of CREATE TABLE ... AS SELECT ...
But SELECT.INTO Statement shows that you can use the SELECT ... INTO newtable FROM ... form. The documentation says this will create newtable in the process.
MS Access doesn't have a "create table as select syntax". Instead, you can use the "select into" syntax:
SELECT column1, column2
INTO new_table_name
FROM existing_table_name
WHERE ...

Drop tables using table names from a SELECT statement, in SQL (Impala)?

How do I drop a few tables (e.g. 1 - 3) using the output of a SELECT statement for the table names? This is probably standard SQL, but specifically I'm using Apache Impala SQL accessed via Apache Zeppelin.
So I have a table called tables_to_drop with a single column called "table_name". This will have one to a few entries in it, each with the name of another temporary table that was generated as the result of other processes. As part of my cleanup I need to drop these temporary tables whose names are listed in the "tables_to_drop" table.
Conceptually I was thinking of an SQL command like:
DROP TABLE (SELECT table_name FROM tables_to_drop);
or:
WITH subquery1 AS (SELECT table_name FROM tables_to_drop) DROP TABLE * FROM subquery1;
Neither of these work (syntax errors). Any ideas please?
even in standard sql this is not possible to do it the way you showed.
in standard sql usually you can use dynamic sql which impala doesn't support.
however you can write an impala script and run it in impala shell but it's going to be complicated for such task, I would prepare the drop statement using select and run it manually if this is one-time thing:
select concat('DROP TABLE IF EXISTS ',table_name) dropstatements
from tables_to_drop

Oracle SQL Developer - Display SELECT output in Query Results window when using PLSQL [duplicate]

This question already has answers here:
How to export query result to csv in Oracle SQL Developer?
(6 answers)
Closed 8 years ago.
I use an Oracle SQL Developer script to do a SELECT, displaying results in Query Results window. I then copy/paste the results into an Excel template for reporting.
I would like to replace the script with a PLSQL block, to allow looping etc. The problem is that simple SELECT FROM (without INTO) doesn't seem to work in PLSQL.
Is there any way to use PLSQL to display the results of a select in a window which I can copy/paste from?
Note: I am disallowed from using EXPORT to create text files directly, which would be much better than copy/paste. There is also a standard Oracle package that does output to a file directly from PLSQL, but I am disallowed from using it, too.
This post was marked as a duplicate of another post, one which asked how to get output from a SELECT that was NOT in a PL/SQL block. I do know how to do that and in fact it's what I am doing currently, as I mentioned in the OP. As I said, SELECT without INTO fails in PL/SQL.
You can create a temporary table:
CREATE GLOBAL TEMPORARY TABLE table_name (
( column1 datatype null/not null,
column2 datatype null/not null,
...
) ON COMMIT DELETE ROWS;
Then through each loop, you can insert your data in it:
INSERT INTO table_name
(SELECT statement);
Finally you can use select statement on temporary table to read data:
SELECT * FROM table_name
and then drop table:
drop table table_name;

Using local variable for "IN" criteria condition with SQL Server [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
SQL : in clause in storedprocedure:how to pass values
I'm using MS SQL Server 2005, and trying to basically script a 2-step process:
Query a table for a list of IDs matching certain criteria
Update a field in that table, where the ID is in the list of IDs returned by the first
With the catch being that steps 1 and 2 might be separated by a considerable time delay and executed in different sessions. Essential the list of IDs used in #2 is historical data: the values which #1 returned at a past point in time.
What I've attempted to do is write all of IDs from #1 into a varchar(8000) in "##, ##, ##, ##," format (this part is working great), and then use that string like:
UPDATE table SET field=newValue WHERE (id IN (#varcharOfCommaSeparatedIDs))
But this is giving me a syntax error, stating that it cannot convert that varchar value into whatever is needed (the error message is being truncated)
Is there a way to do this without putting the entire SQL command into a string and executing that (using EXEC or sp_executesql)? After years of avoiding injection attacks I have a somewhat instinctive (and perhaps irrational) aversion to "dynamic SQL"
If you're passing the values around between SP's on the SQL Server, I highly recommend storing the values in tables...
- Temp Tables (#mytable)
- Table Variables (#table)
- Real Tables
In SQL Server 2008 onwards you can have table valued input parameters...
If you're passing the values in from an app, the dread comma-separated-string is indeed useful. There are many answers on SO that give Table Valued Functions for turning a string into a table of ids, read to be joined on.
SELECT
*
FROM
foo
INNER JOIN
dbo.bar(#mystring) AS bar
ON foo.id = bar.id
Just write it out to a table.
IF EXISTS (SELECT 1 FROM Database.dbo.MyHoldingTable)
DROP TABLE Database.dbo.MyHoldingTable
SELECT <fields>
INTO Database.dbo.MyHoldingTable
FROM <other table>
WHERE <conditions>
Then, later:
UPDATE OtherTable
Set Column=NewValue
WHERE ID IN (SELECT id FROM Database.dbo.MyHoldingTable)
Also note you could also use an INNER JOIN on your table instead of a IN clause if you prefer.

Possible to exclude or reorder a column from `*`? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
SQL exclude a column using SELECT * [except columnA] FROM tableA?
Is it possible to exclude a column from a select * from table statement with SQL Server?
I have a need for this and this is my only option other than parsing a raw SQL string to get out the required field names (I really don't want to do that).
Just to be bold. When the query is made I do not have access to the list of fields needed from the table but I do know which field I do not need. This is part of a complex multi-part query.
Surely there must be some way even if it's "hackish" such as using table variables or views
My other option is to reorder the columns. My problem is with ExecuteScalar SQL functions which get the first row and first column.
EDIT
I can't add an answer since this is now closed but the way I ended up doing it was like so:
;with results_cte as (
select (calculation) as calculated_column, * from table
)
select * into #temptable from results_cte
where calculated_column<10 /*or whatever*/
alter table #temptable
drop column calculated_column
select * from #temptable
drop table #temptable
Nope. You'll have to build your statement manually or just select *.
No.
Instead, you could check syscolumns to get all of the field names, or (perhaps) SELECT * and ignore that column.
If you use dynamic SQL, you can generate the query from metadata about the table or view (INFORMATION_SCHEMA.COLUMNS) and exclude columns that way. I do this a lot to generate triggers or views.
But there is nothing in the SQL language which supports this.
The best way to handle this would be to select * and then just not present the excluded column to your users in your frontend. As others have noted, SQL has no direct capability of doing an all-columns-except construct.