SQL select with "" netbeans without "" - sql

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.

Related

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

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

How to copy a table schema with sql based on existance of the table?

I want to duplicate a table schema, but only if the target table does not yet exist:
CREATE TABLE IF NOT EXISTS my_table_secondary as select * from my_table_primary
Problem: running this if the my_secondary_table exists results in:
ERROR: Syntaxerror at "as".
What could I do to make this sql statement work?
(postgres 9.4)
To duplicate the schema of a table, you need a completely different syntax.
create table if not exists my_table_secondary (
like my_table_primary including all
);
INCLUDING ALL is an abbreviated form of INCLUDING DEFAULTS INCLUDING
CONSTRAINTS INCLUDING INDEXES INCLUDING STORAGE INCLUDING COMMENTS.
It doesn't duplicate foreign key constraints. See CREATE TABLE. Search for "The LIKE clause".
Try this approach:
CREATE OR REPLACE VIEW my_view AS SELECT * FROM my_table_primary;
CREATE TABLE IF NOT EXISTS my_table_secondary LIKE my_view;
INSERT INTO my_table_secondary
SELECT * FROM my_view
WHERE NOT EXISTS ( SELECT * FROM my_table_secondary );
More on CREATE TABLE ... LIKE: http://www.postgresql.org/docs/9.1/static/sql-createtable.html

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 ............

Oracle view with multiple join is only recognize when use a quotes around - why?

I have encountered a strange behavior while executing an sql query on the Oracle view.
The view contains multiple joins. When I type a regular sql:
select * from vView - I receive the error that view is not found
select * from "vView" - The query is executed.
I am wondering why ?
Below is my sql:
CREATE OR REPLACE FORCE VIEW "TMSCODE"."vCountEventsData" ("EV_ID_NUMBER", "SI_ID", "EV_YEAR", "EV_INS_DATE", "EV_REM_DATE", "EV_AADT_TOT", "EV_AADT_DIR1", "EV_AADT_DIR2", "EV_REPORT", "DIRECTION", "CNAME", "STATION_DESC") AS
SELECT
"TMSCODE"."STC_EVENTS".EV_ID_NUMBER,
"TMSCODE"."STC_EVENTS".SI_ID,
"TMSCODE"."STC_EVENTS".EV_YEAR,
"TMSCODE"."STC_EVENTS".EV_INS_DATE,
"TMSCODE"."STC_EVENTS".EV_REM_DATE,
"TMSCODE"."STC_EVENTS".EV_AADT_TOT,
"TMSCODE"."STC_EVENTS".EV_AADT_DIR1,
"TMSCODE"."STC_EVENTS".EV_AADT_DIR2,
"TMSCODE"."STC_EVENTS".EV_REPORT,
"TMSCODE"."D_DIRECTION".DIRECTION,
"TMSCODE"."D_CONSULTANT".CNAME,
"TMSCODE"."D_STATION_TYPE".STATION_DESC
FROM
"TMSCODE"."STC_EVENTS"
INNER JOIN "TMSCODE"."D_DIRECTION" ON ("TMSCODE"."STC_EVENTS".EV_DIR = "TMSCODE"."D_DIRECTION".ID)
INNER JOIN "TMSCODE"."D_CONSULTANT" ON ("TMSCODE"."STC_EVENTS".EV_CONS = "TMSCODE"."D_CONSULTANT".ID)
INNER JOIN "TMSCODE"."D_STATION_TYPE" ON ("TMSCODE"."STC_EVENTS".EV_STATION_TYPE = "TMSCODE"."D_STATION_TYPE".ID)
WITH READ ONLY
The view was created with a mixed case name. If you issue the following (note no quotes around object names)
create view karl.vView
as
(select * from dba_tables);
The RDBMS will create the view and you will then find a line in dba_views (or user_views if you can't see dba_views) with the name VVIEW in upper case. Then select * from karl.vview or ... from KARL.VVIEW will work
If however you quote the objects names retains the case and you have to explicitly match it again with quotes. (This will also allow spaces in names and other bad scenarios. Worth knowing about to avoid and to be able to resolve when it does happen.
SYS#icedev> create table "TesT" (a int);
Table created.
SYS#icedev> insert into TesT values (1);
insert into TesT values (1)
*
ERROR at line 1:
ORA-00942: table or view does not exist
SYS#icedev> insert into test values (1);
insert into test values (1)
*
ERROR at line 1:
ORA-00942: table or view does not exist
SYS#icedev> insert into "TesT" values (1);
1 row created.
Of course to drop this table I had to use Drop table "TesT";
In Oracle, objects have an uppercase name unless quoted.
You used quotes to create an object with a mixed case name, so you must now ALWAYS refer to the object using a quoted identifier to say "Don't change this to uppercase".

How can I create a copy of an Oracle table without copying the data?

I know the statement:
create table xyz_new as select * from xyz;
Which copies the structure and the data, but what if I just want the structure?
Just use a where clause that won't select any rows:
create table xyz_new as select * from xyz where 1=0;
Limitations
The following things will not be copied to the new table:
sequences
triggers
indexes
some constraints may not be copied
materialized view logs
This also does not handle partitions
I used the method that you accepted a lot, but as someone pointed out it doesn't duplicate constraints (except for NOT NULL, I think).
A more advanced method if you want to duplicate the full structure is:
SET LONG 5000
SELECT dbms_metadata.get_ddl( 'TABLE', 'MY_TABLE_NAME' ) FROM DUAL;
This will give you the full create statement text which you can modify as you wish for creating the new table. You would have to change the names of the table and all constraints of course.
(You could also do this in older versions using EXP/IMP, but it's much easier now.)
Edited to add
If the table you are after is in a different schema:
SELECT dbms_metadata.get_ddl( 'TABLE', 'MY_TABLE_NAME', 'OTHER_SCHEMA_NAME' ) FROM DUAL;
create table xyz_new as select * from xyz where rownum = -1;
To avoid iterate again and again and insert nothing based on the condition where 1=2
Using sql developer select the table and click on the DDL tab
You can use that code to create a new table with no data when you run it in a sql worksheet
sqldeveloper is a free to use app from oracle.
If the table has sequences or triggers the ddl will sometimes generate those for you too. You just have to be careful what order you make them in and know when to turn the triggers on or off.
You can do this
Create table New_table as select * from Old_table where 1=2 ;
but be careful
The table you create does not have any Index, PK and so on like the old_table.
DECLARE
l_ddl VARCHAR2 (32767);
BEGIN
l_ddl := REPLACE (
REPLACE (
DBMS_LOB.SUBSTR (DBMS_METADATA.get_ddl ('TABLE', 'ACTIVITY_LOG', 'OLDSCHEMA'))
, q'["OLDSCHEMA"]'
, q'["NEWSCHEMA"]'
)
, q'["OLDTABLSPACE"]'
, q'["NEWTABLESPACE"]'
);
EXECUTE IMMEDIATE l_ddl;
END;
Simply write a query like:
create table new_table as select * from old_table where 1=2;
where new_table is the name of the new table that you want to create and old_table is the name of the existing table whose structure you want to copy, this will copy only structure.
SELECT * INTO newtable
FROM oldtable
WHERE 1 = 0;
Create a new, empty table using the schema of another. Just add a WHERE clause that causes the query to return no data:
WHERE 1 = 0 or similar false conditions work, but I dislike how they look. Marginally cleaner code for Oracle 12c+ IMHO is
CREATE TABLE bar AS
SELECT *
FROM foo
FETCH FIRST 0 ROWS ONLY;
Same limitations apply: only column definitions and their nullability are copied into a new table.
If one needs to create a table (with an empty structure) just to EXCHANGE PARTITION, it is best to use the "..FOR EXCHANGE.." clause. It's available only from Oracle version 12.2 onwards though.
CREATE TABLE t1_temp FOR EXCHANGE WITH TABLE t1;
This addresses 'ORA-14097' during the 'exchange partition' seamlessly if table structures are not exactly copied by normal CTAS operation. I have seen Oracle missing some of the "DEFAULT" column and "HIDDEN" columns definitions from the original table.
ORA-14097: column type or size mismatch in ALTER TABLE EXCHANGE
PARTITION
See this for further read...
you can also do a
create table abc_new as select * from abc;
then truncate the table abc_new. Hope this will suffice your requirement.
Using pl/sql developer you can right click on the table_name either in the sql workspace or in the object explorer, than click on "view" and than click "view sql" which generates the sql script to create the table along with all the constraints, indexes, partitions etc..
Next you run the script using the new_table_name
copy without table data
create table <target_table> as select * from <source_table> where 1=2;
copy with table data
create table <target_table> as select * from <source_table>;
In other way you can get ddl of table creation from command listed below, and execute the creation.
SELECT DBMS_METADATA.GET_DDL('TYPE','OBJECT_NAME','DATA_BASE_USER') TEXT FROM DUAL
TYPE is TABLE,PROCEDURE etc.
With this command you can get majority of ddl from database objects.
Create table target_table
As
Select *
from source_table
where 1=2;
Source_table is the table u wanna copy the structure of.
create table xyz_new as select * from xyz;
-- This will create table and copy all data.
delete from xyz_new;
-- This will have same table structure but all data copied will be deleted.
If you want to overcome the limitations specified by answer:
How can I create a copy of an Oracle table without copying the data?
The task above can be completed in two simple steps.
STEP 1:
CREATE table new_table_name AS(Select * from old_table_name);
The query above creates a duplicate of a table (with contents as well).
To get the structure, delete the contents of the table using.
STEP 2:
DELETE * FROM new_table_name.
Hope this solves your problem. And thanks to the earlier posts. Gave me a lot of insight.