SELECT INTO using Oracle - sql

I'm trying to do a SELECT INTO using Oracle. My query is:
SELECT * INTO new_table FROM old_table;
But I get the following error:
SQL Error: ORA-00905: missing keyword
00905. 00000 - "missing keyword"
Any ideas what's wrong?
The Standard behavior of the above should be as I originally thought:
However Oracle implemented it totally differently in their own dialect of SQL
Oracle Docs on Insert ... Select

If NEW_TABLE already exists then ...
insert into new_table
select * from old_table
/
If you want to create NEW_TABLE based on the records in OLD_TABLE ...
create table new_table as
select * from old_table
/
If the purpose is to create a new but empty table then use a WHERE clause with a condition which can never be true:
create table new_table as
select * from old_table
where 1 = 2
/
Remember that CREATE TABLE ... AS SELECT creates only a table with the same projection as the source table. The new table does not have any constraints, triggers or indexes which the original table might have. Those still have to be added manually (if they are required).

select into is used in pl/sql to set a variable to field values. Instead, use
create table new_table as select * from old_table

Use:
create table new_table_name
as
select column_name,[more columns] from Existed_table;
Example:
create table dept
as
select empno, ename from emp;
If the table already exists:
insert into new_tablename select columns_list from Existed_table;

Try using this below statement instead of using select into in oracle:
select * from(select * from table1) table2 ;

Related

Insert Statement - From Temp Table

I am currently trying to insert data into a Redshift table from multiple temporary tables that I have created.
Insert into schema.table1 (
with a as (
select t1.country_code, t1.country_name
from t1
)
select * from a
);
The error that I get on this statement says the following: Amazon Invalid operation: syntax error at or near "as";. What do I need to change in order to be able to insert data from a temp table?
Is it not possible to run the command like this if you have same table structures in both schema.table1 and t1
insert into schema.table1
select t1.country_code, t1.country_name
from t1;
One other thing you might want to check is, in your SQL table1 is in 'schema' but t1 is referred without a schema so it's in public, make sure you don't have two t1s with different structures.
I just tried this and it worked for me.
insert into tempt1 ( with a as (select a from tempt2) select * from a);

Join two datasets by two columns in PostgreSQL

I have a PostgreSQL query, where I create temp table by joining another temp table, and table from my database.
DROP TABLE IF EXISTS other_temp_table;
CREATE TEMP TABLE other_temp_table AS
SELECT *
FROM base.main_data
WHERE period_start_time::DATE >= '2017-06-20' AND period_start_time::DATE <= '2017-07-26';
------------------------------------------------------------------------------------------------------------------------
DROP TABLE IF EXISTS first_temp_table;
CREATE TEMP TABLE first_temp_table AS
SELECT *
FROM _temp_table
LEFT JOIN base."UL_parameters"
ON temp_table.base_col::INT = base."UL_parameters".base_col::INT
and temp_table.sec_col::INT= base."UL_parameters".sec_col::INT;
Now, the problem is `ERROR: column "sec_col" specified more than once.
But when I delete sec_col join condition and do just base_col, everything is ok. I think that I need to create an alias, but not sure how.
I think that the problem is that there is a sec_col column in both joined tables. Try to replace select * with select column1, column2, ....
It's a good practice to avoid select * in general, because it can cause errors when the table definition changes.

Regex put name schema for 'table' in select query syntax

How can I put with regex, schema name table, for query select?
Query doesn't have fixed size.
Example:
select * from t1 union select * from t2
Result:
select * from schema1.t1 union select * from schema1.t2
Thanks!
(1) If you are using it in a code, maybe you can do a search and replace at front end as Luk suggested.
(2) Alternatively, in your current session you can set default schema as schema1 so your query would work without schema name. But it would depend on your database name.
Like in Oracle you can do
ALTER SESSION SET CURRENT_SCHEMA=schema1
Now select * from t1 would effectively mean select * from schema1.t1.
You can search how to set default schema in <your database> in google and you would get syntax for other databases as well.

SQL nesting queries

I wish to write two nested queries of create and select statements. My motive is to
create a new table which includes columns and entries from two other tables.
I wrote a query but it is giving me an error.
create table table_3(select * from table_1,table_2)
For SQL Server you can use:
SELECT *
INTO table_3
FROM table_1, table_2
If you want to join the two tables based on some key then:
SELECT *
INTO table_3
FROM table_1 JOIN table_2 on table_1.ID = table_2.FKID
You may see: SQL SERVER – CTAS – Create Table As SELECT – What is CTAS?
This can be used to create another table of same type
CREATE TABLE new_table
AS (SELECT * FROM old_table);

SQL Insert/Update Issue

I am trying to update one table from another, im able to update fine as long as the customer record exists, but there are some entries that dont.
To solve this i've tried running the following insert
SELECT *
INTO SalBudgetCust
FROM SalBudgetCust_temp
WHERE NOT EXISTS (
SELECT Customer
FROM SalBudgetCust
WHERE Customer = SalBudgetCust_temp.Customer
)
but im prompted with
There is already an object named 'SalBudgetCust' in the database.
Im stuck at this point... could anyone offer a little guideance?
SELECT INTO implicitly creates the table you name. You should instead use INSERT INTO ... SELECT * FROM ..., so that the existing table is used.
It should be INSERT INTO instead of SELECT * INTO ... like
INSERT INTO SalBudgetCust SELECT * FROM SalBudgetCust_temp
WHERE NOT EXISTS
(
SELECT Customer FROM SalBudgetCust WHERE Customer = SalBudgetCust_temp.Customer
)
The general syntax to insert data of one table into another is :
INSERT INTO new_table
SELECT * FROM old_table
WHERE some_condition;
Where, new_table is the table where you want to insert data, old_table is table from where you are fetching data and some_condition is the expression / condition based upon which you want to fetch data from old table.
You may use other clauses like order by, group by, and even sub queries after where clause.
May refer this SQL INSERT INTO and it's subsequent pages.