Oracle SQL Developer combine using "WITH AS", "INSERT INTO", SELECT" - sql

I am trying to figure out a way to do this.
I have a table with only read access, so let's call it existing_table.
I want to have a reference table using "with as" statement and insert a new row to my reference table.
My code is: (pretend existing_table is ready for use)
INSERT INTO NEW_TABLE ( COLUMN_A, COLUMN_B)
VALUES (1, 'A')
WITH NEW_TABLE
AS (SELECT * from EXISTING_TABLE)
SELECT * from NEW_TABLE
However, it doesn't work. Help please!!!!! "WITH" is where it gives me the error.
if I move insert into statement after with as then "INSERT" is where it gives me the error.
My question is how can I use with/insert/select statement?

Change the name of the identifier to some other name in WITH Clause as you have same name leading to ambiguity. Either way I guess you want like
Create Table
NEW_TABLE AS SELECT *
FROM EXISTING_TABLE;
SELECT * FROM NEW_TABLE

Related

INSERT INTO audit table when SELECT from table

Is that applicable to use INSERT inside SELECT statement, in order to insert some data whenever we select. the scenarion is i need to audit the select operations so whenever we run SELECT statemnt we need to insert into a nother table, i have all the code for the select operation all what i need is to find a way to add INSERT statement inside SELECT statement
EX
SELECT *, (
INSERT INTO auditTable(ID, CREATEDINFO) VALUES ( :v0, :v1)
) FROM mainTable;
As suggested in the comments, there may be better ways to do what you are asking, but for the example you have shown, you want to do something like this:
INSERT INTO auditTable(ID, CREATEDINFO)
SELECT *
FROM mainTable;
Note: I'm assuming there are only two rows in mainTable as your query suggests, otherwise you should specify which rows you are selecting rather than using *

Difference between INSERT INTO and SELECT INTO when calling OPENROWSET

select 1 as X,d.* into [TravelData] from OPENROWSET('SQLNCLI','Server=<redacted>',
'exec [OtherDB].[GetTravelData] 1, ''28-Nov-2016 16:00'', ''28-Nov-2016 19:00''') as d
I have this as a way to slurp remote DB into a local table. This syntax appears to work BUT I get error:
There is already an object named 'TravelData' in the database.
Makes sense, SELECT INTO is supposed to create the table. But thinking I'd simply change SELECT to INSERT I then get syntax errors. What should the correct syntax be to get this data into an existing DB table whose structure matches the query output?
It has nothing to do with using OPENROWSET.
INSERT INTO ... requires that the table already exist.
SELECT ... INTO requires that the table not exist. The table will be created by the statement using the columns defined in the SELECT.
Here is the INSERT INTO SELECT syntax
INSERT INTO [TravelData]
(X,
col1,
col2,
...)
SELECT 1 AS X,
d.col1,
d.col2,
.....
FROM OPENROWSET('SQLNCLI',
'Server=<redacted>',
'exec [OtherDB].[GetTravelData] 1, ''28-Nov-2016 16:00'', ''28-Nov-2016 19:00''') AS d
Note : Instead of * in select list add the column list. Also in Insert mention the column list

SQL select with "" netbeans without ""

I'm trying to bind data from SQL table to JTable using netbeans, but my database is reacting only if select is written as SELECT * FROM "table" and netbeans is using SELECT * FROM table without "". Can you tell me how to change it in netbeans to use "" or in Oracle SQL to don't need "?
When you use double quotes and lower case, table name is case sensitive . If not, Oracle converts it to upper case, and makes it non case sensitive . For instance,
create table "table1" (id int not null);
select * from table1 ; -- ORA-00942: table or view does not exist
select * from TABLE1; -- ORA-00942: table or view does not exist
select * from "table1"; --ok
----------------------------
create table table2 (id int not null); -- or TABLE2, or even "TABLE2"
select * from table2 ; -- ok
select * from TABLE2; --ok
select * from "TABLE2"; --ok
The same rule applied to other object names (such as fields, functions, procedures, packages, etc).
I'm not very familiar with netbeans but you usually should not need to put the table-name in quotes.
It sounds like your table-name has special characters in it. Have you tried it with another table?
Also your actual query would be useful.

copy the tables after execution

I would like to create new table after executing that query
create table newTable as select * from oldTable
However, this does not appear to work. How do I get the new table after executing some queries?
The syntax in general is like:
CREATE TABLE new_table
AS (SELECT * FROM old_table);
For example:
CREATE TABLE suppliers
AS (SELECT id, address, city, state, zip
FROM companies
WHERE id > 1000);
Try removing the stars (*) and add the brackets.
Read here for more examples.
I am not sure what DBMS you are using or what errors you are getting, so I will try to answer for multiple systems.
If you are working with Oracle or PostgreSQL (there might be some other systems that this rule applies to), your syntax seems to be correct. Just make sure your new table doesn't exist yet - otherwise it's going to error out. In case if you are trying to insert into an existing table - which I don't think the case is, however - you can try something like -
INSERT INTO newTable SELECT * FROM oldTable
On the other hand, if you are working with T-SQL (SQL Server), you could SELECT INTO the new table. The new table will be created with the old table's schema.
You can read more about the INTO Clause at MSDN Library.
Your code should look like -
SELECT *
INTO newTable
FROM oldTable
And, specifying the column names and filters also works the similar way -
SELECT Column1, Column2, Column3, ...
INTO newTable
FROM oldTable
WHERE <Filter Condition>
Whatever the case is, you would get more help if you specify the details.
As You Said you want to copy the values into new table after execution
whether if you are running the stored procedure using cursor let the cursor shuld be closed then use query as follows
Select * into Table1 from Table2
if you want to copy selected colums go for
Select Coloumn1 ,column2,... into table1 from table 2 where ............

Insert result set from UNPIVOT into table

I am trying to find the syntax for inserting the results from an UNPIVOT statement into an existing table in the database?
simplest answer which works for any SELECT including UNPIVOT would be...
INSERT INTO MyTable
SELECT statement
However, this does require that your destination tables columns match your SELECT statement columns.
Although you can get around this limitation with...
INSERT INTO MyTable (column1, column2....)
SELECT statement