I'm trying to create temporary table in PL/SQL developer and insert some data, but it throws error:
ORA-00905
My code:
CREATE PRIVATE TEMPORARY TABLE my_temp_table (
id NUMBER,
description VARCHAR2(20)
);
CREATE PRIVATE TEMPORARY TABLE introduced only in Oracle 18:
Oracle 18c added private temporary tables, which are single-session in-memory objects.
In previous version, you can create global temporary table:
CREATE GLOBAL TEMPORARY TABLE my_temp_table (
id NUMBER,
description VARCHAR2(20)
);
Related
I have a table blobtest which was created with the following code:
create table blobtest(
id number,
file_data blob);
I want to insert data representing a file on my computer into this table. How may I do this?
How do I (or rather, what do I write) a SQL CREATE TABLE command to create a table for Term as defined in the picture to create a table?
I am completely new to mhphpadmin (sql) and am learning this from scratch.
The general syntax is
Create Table Table_name (Column_name1 DataType, Column_name2 DataType,...)
You can create table as
CREATE TABLE Term (Term_Name VARCHAR(25), Term_StartDate DATE, TermEndDate DATE);
How do I create (in a single SQL command) a schema and a table in it, but with the table containing a serial column (in Postgres) ?
For example, here I am attempting to create schema zoo with table animals from type animal_t with serial column animal_id:
DROP TYPE IF EXISTS animal_t CASCADE;
CREATE TYPE animal_t AS (
animal_id integer,
animal_name varchar
);
CREATE SCHEMA zoo
CREATE TABLE animals OF animal_t
animal_id WITH OPTIONS NOT NULL DEFAULT nextval('animals_animal_id_seq')
CREATE SEQUENCE animals_animal_id_seq OWNED by animals.animal_id
;
Notes:
CREATE SCHEMA only accepts CREATE TABLE or CREATE SEQUENCE, it does not accept ALTER , this is why I have to do all of this in a single SQL sentence.
Result:
-bash-4.3$ psql dev < animal.sql
DROP TYPE
CREATE TYPE
ERROR: syntax error at or near "animal_id"
LINE 3: animal_id WITH OPTIONS NOT NULL DEFAULT nextval('animals_a...
^
-bash-4.3$
You have two options
simplify your statement and get rid of the object type
CREATE SCHEMA zoo
CREATE table animal
(
animal_id serial,
animal_name varchar
);
use a search path if you want to avoid to prefix the table with a schema name:
DROP TYPE IF EXISTS animal_t CASCADE;
CREATE TYPE animal_t AS (
animal_id integer,
animal_name varchar
);
CREATE SCHEMA zoo;
set search_path = zoo;
CREATE SEQUENCE animals_animal_id_seq;
CREATE TABLE animals OF animal_t
animal_id WITH OPTIONS NOT NULL DEFAULT nextval('animals_animal_id_seq');
alter sequence animals_animal_id_seq owned by animals.animal_id;
After creating the type and the schema, the current schema is set to the just created one, so all subsequent statements use zoo as the default schema.
Note that you can do this in a single transaction if that is another reason for you to use the "extended" create schema syntax.
I think the following should do it. The idea is to ALTER schema_name.thing:
CREATE SCHEMA zoo
CREATE TABLE animals OF animal_t ( animal_id WITH OPTIONS NOT NULL )
;
CREATE SEQUENCE zoo.animals_animal_id_seq OWNED BY zoo.animals.animal_id;
ALTER TABLE zoo.animals ALTER animal_id SET DEFAULT nextval('zoo.animals_animal_id_seq');
I am restoring the data of a truncated table in an Oracle Database from an exported csv file. However, I find that the primary key auto-increments and does not insert the actual values of the primary key constrained column from the backed up file.
I intend to do the following:
1. drop the primary key
2. import the table data
3. add primary key constraints on the required column
Is this a good approach? If not, what is recommended? Thanks.
EDIT: After more investigation, I observed there's a trigger to generate nextval on a sequence to be inserted into the primary key column. This is the source of the predicament. Hence, following the procedure above would not solve the problem. It lies in the trigger (and/or sequence) on the table. This is solved!
easier to use your .csv as an external table and then go
create table your_table_temp as select * from external table
examine the data in the new temp table to ensure you know what range of primary keys is present
do a merge into the new table
samples from here and here
CREATE TABLE countries_ext (
country_code VARCHAR2(5),
country_name VARCHAR2(50),
country_language VARCHAR2(50)
)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY ext_tab_data
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
(
country_code CHAR(5),
country_name CHAR(50),
country_language CHAR(50)
)
)
LOCATION ('Countries1.txt','Countries2.txt')
)
PARALLEL 5
REJECT LIMIT UNLIMITED;
and the merge
MERGE INTO employees e
USING hr_records h
ON (e.id = h.emp_id)
WHEN MATCHED THEN
UPDATE SET e.address = h.address
WHEN NOT MATCHED THEN
INSERT (id, address)
VALUES (h.emp_id, h.address);
Edit: after you have merged the data you can drop the temp table and the result is your previous table with the old data and the new data together
Edit you mention " During imports, the primary key column does not insert from the file, but auto-increments". This can only happen when there is a trigger on the table, likely, Before insert on each row. Disable the trigger and then do your import. Re-enable the trigger after committing your inserts.
I used the following procedure to solve it:
drop trigger trigger_name
Imported the table data into target table
drop sequence sequence_name
CREATE SEQUENCE SEQ_NAME INCREMENT BY 1 START WITH start_index_for_next_val MAXVALUE max_val MINVALUE 1 NOCYCLECACHE 20 NOORDER
CREATE OR REPLACE TRIGGER "schema_name"."trigger_name"
before insert on target_table
for each row
begin
select seq_name.nextval
into :new.unique_column_name
from dual;
end;
How can I create a temporary table without first creating the columns?
CREATE TABLE #Yaks (
YakID int,
YakName char(30) )
select name
from tempdb..sysobjects
where name like '#yak%'
drop table #yaks
It is a pain to have to define the table first.
Create a (temp) table with the same columns as another (no data copied):
select * into #TempTable
from MyTable
where 1=0
Note: Does not create any Foreign keys, indexes etc...