How do I insert into a pre-existing rows from another table for a whole column Oracle SQL? - oracle

I am trying to insert postal codes from one table(insert into pre-existing rows) to another table of addresses that have null postal codes. The address_id in postalcodes corresponds to the address_id in address table.
The postal codes table has the address_id and postalcode.
Here is the postal codes table:
Here is the address table:
This is what I have tried.
INSERT INTO address ( postal_code )
SELECT
postalcodes.postalcode
FROM
postalcodes
WHERE
address.address_id = postalcodes.address_id;
But it gives me an error:
Error starting at line : 26 in command -
INSERT INTO address ( postal_code )
SELECT
postalcodes.postalcode
FROM
postalcodes
WHERE
address.address_id = postalcodes.address_id
Error at Command Line : 32 Column : 9
Error report -
SQL Error: ORA-00904: "ADDRESS"."ADDRESS_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:

You want an update, not an insert:
UPDATE address
SET postal_code = (SELECT p.postal_code
FROM postalcodes p
WHERE a.address_id = p.address_id
)
WHERE postal_code IS NULL;

You need to insert the rest of the values for address. You can't insert postal codes by themselves. Or are you trying to update the values of zipcodes for existing addresses?. Creating zip codes into address will give you NULL, NULL, NULL, ZIP_CODE, NULL, ECT. IF it doesnt give you an error based on some primary key constrain
Maybe trying selecting
FROM
postalcodes, Address
SQL hAs no ida what address is unless you select from it.

Related

ORA-01722: invalid number - type issue in oracle

I have a problem with data type and relationship between two tables
let's assume I have two tables as shown in the image
First table: Attached _file. The ID coulmn is number
Second table: FN_Transaction. the ID coulmn is varchar(20)
The ID in FN_Transaction has four cases
ID is null which means there is no attached file
ID has text as 'No' cell which means there is no attached file
ID starts with 0, but it will be equaled ID in Attached _file table such as 01222 = 1222
ID in FN_Transaction equals ID in Attached _file without 0 such as 2344 = 2344
let's see the query
with docs as
(
SELECT
ID,
COUNT(type) fileNo,
MAX(Date) date
FROM
Attached_file
GROUP BY
ID
)
Select
InvoiceNo,
fileNo,
date,
Amount
from
FN_transaction x1
left join docs x2 on
(Trim(x1.ID) = x2.ID)
the error that I got
Error report -
SQL Error: ORA-12801: error signaled in parallel query server P034, (1)
ORA-01722: invalid number
12801. 00000 - "error signaled in parallel query server %s"
*Cause: A parallel query server reached an exception condition.
*Action: Check the following error message for the cause, and consult
your error manual for the appropriate action.
Do you have an idea about how to handle this type of column?
Please advise
You can use the default on conversion error clause in TO_NUMBER function (introduced in 12.2) as follows:
ON ( to_number(X1.ID default null on conversion error) = X2.ID )
Note that 01222 will be automatically converted to a number 1222. character NO will be converted to NULL because of default on conversion error and it will not match the join condition(which satisfies your requirement)

ORA-00909 error when Insert object in object table

I am getting an error ORA-00909 in when inserting an object of a superType in an object table. These are the definitions for the objects:
CREATE OR REPLACE TYPE address AS OBJECT (
street VARCHAR(20),
country VARCHAR(20),
province VARCHAR(20),
city VARCHAR2(20),
zipcode VARCHAR(10)
) FINAL;
CREATE OR REPLACE TYPE company AS OBJECT (
CIF VARCHAR2(9),
code VARCHAR2(10),
name VARCHAR2(20),
signUpDate DATE,
email VARCHAR2(20),
adminAddress address
) NOT FINAL;
CREATE OR REPLACE TYPE inCourseCompany UNDER company (
postalAddress address,
numEmployees NUMBER
) FINAL;
And the object table:
CREATE TABLE companies_objtab OF company (PRIMARY KEY CIF) OBJECT IDENTIFIER IS PRIMARY KEY;
I try to insert an object with the following statement:
INSERT INTO companies_objtab VALUES (
company('J12345678','000001','Test Company',TO_DATE(sysdate, 'dd/mm/yyyy hh24:mi:ss'),'',address('','','','',''))
);
and I get error Error
SQL: ORA-00909: invalid number of arguments
00909. 00000 - "invalid number of arguments"
However, when I insert an object of the subtype inCourseCompany it is inserted correctly:
INSERT INTO companies_objtab VALUES (
inCourseCompany('G11111111','','',TO_DATE(sysdate, 'dd/mm/yyyy hh24:mi:ss'),'',address('','','','',''), address('','','','',''), 100)
);
Any hint about what causes the error?
I am using Oracle SQL Developer 4.0.2.15.21 and Oracle Database Express Edition 11g Release 2.
Thank you in advance.
i tried all your statements right as you posted them (copy-paste). Both on 12c and 11gR2. Everything worked, both inserts. The only thing I noticed is that your create table is a bit incorrect. This is the correct one (but this does not explain the error you get). HTH KR
CREATE TABLE companies_objtab OF company (CIF PRIMARY KEY) OBJECT IDENTIFIER IS PRIMARY KEY;
Maybe try dropping the table and creating it again.

oracle cannot insert values into a nested table field

I have the sample CUSTOMERS table.
I exported the values from table as INSERT script.
I took one row, modified the value from PK and re-executed the insert.
Insert into oe.customers (CUSTOMER_ID,
CUST_FIRST_NAME,
CUST_LAST_NAME,
CUST_ADDRESS,
PHONE_NUMBERS,
NLS_LANGUAGE,
NLS_TERRITORY,
CREDIT_LIMIT,
CUST_EMAIL,
ACCOUNT_MGR_ID,
CUST_GEO_LOCATION,
DATE_OF_BIRTH,
MARITAL_STATUS,
GENDER,
INCOME_LEVEL,
CREDIT_CARDS)
values ( oe.customer_seq.nextval,
'Donald',
'Hunter',
OE.CUST_ADDRESS_TYP('5122 Sinclair Ln','21206','Baltimore','MD','US'),
OE.PHONE_LIST_TYP('+1 410 123 4795'),
'us',
'AMERICA',
2600,
'Donald.Hunter#CHACHALACA.EXAMPLE.COM',
145,
MDSYS.SDO_GEOMETRY(2001,8307,MDSYS.SDO_POINT_TYPE(-76.545732,39.322775,NULL),NULL,NULL),
to_date('20-JAN-60','DD-MON-RR'),
'married',
'M',
'G: 130,000 - 149,999',
OE.TYP_CR_CARD_NST(OE.TYP_CR_CARD('Visa',100000000000011)));
and I have the following error message:
Error at Command Line : 32 Column : 29 Error report - SQL Error:
ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
which refers to line:
OE.TYP_CR_CARD_NST(OE.TYP_CR_CARD('Visa',100000000000011)));
The definition of types are:
create or replace type typ_cr_card as object
( card_type VARCHAR2(25)
, card_num NUMBER);
create or replace type typ_cr_card_nst as table of typ_cr_card;
Can someone tell me, please, what is wrong with this insert line as it is the one provided by SQL DEVELOPER?
NOTE: Also, I tried to use a procedure which inserts values in this table and the error refers to TYP_CR_CARD_NST datatype.

SQL Error: ORA-01722: invalid number

I have enclosed the table and the insert statement I am working on.
CREATE TABLE EMP (
DRIVER_ID INTEGER NOT NULL
, FNAME VARCHAR(30) NOT NULL
, LNAME VARCHAR(30) NOT NULL
, ADDRESS VARCHAR(50) NOT NULL
, SALARY VARCHAR(50) NOT NULL
, DOB DATE NOT NULL
, SHIFTS VARCHAR2(20) NOT NULL
, SSN CHAR(11) NOT NULL
, PHONE INTEGER NOT NULL
, HIRING_DATE DATE NOT NULL
, EMAIL VARCHAR2(50) NOT NULL
);
When I run this insert statement:
INSERT INTO EMP (DRIVER_ID, FNAME, LNAME, ADDRESS, SALARY, DOB, SHIFTS, SSN, PHONE, HIRING_DATE, EMAIL)
VALUES (SEQ_EMP.NEXTVAL,'Emma', 'Johnson', '123 Main Street', 'DIRECT DEPOSIT', '31 JANUARY,1988', 'MORNING', '579-45-6666', '410-555-1112', '16 DECEMBER,2013', 'ejohnson#fakemail.com');
I get this error message
SQL Error: ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
Lets look at this logically. The error message is saying "invalid number". Oracle is saying to you "I am expecting a number, but you gave me something that isn't a number".
Looking at the table SQL, you can see that the table has two columns whose type is a number type (actually INTEGER). These are DRIVER_ID and PHONE. (The other columns don't matter now ... because they won't expect a number as the value.)
Now look at the insert SQL, and the values corresponding to those columns.
The value inserted into the DRIVER_ID column comes from SEQ_EMP.NEXTVAL ... which I would assume has type INTEGER. That means, you won't get an error from there.
The value inserted into the PHONE column is '410-555-1112'. But, hey, that isn't a number. Its a string! And besides a (mathematical) number doesn't have hyphen characters embedded in it!
In short, if you are going to store phone numbers with - (or + or space) characters embedded in them, you can't use INTEGER as the column type.
In your table DDL change
PHONE INTEGER NOT NULL
to
PHONE CHAR(12) NOT NULL
as previously mentioned by user Stephen C using CHAR instead of INTEGER.
Also, those DATE fields will have to be converted to CHAR datatypes as well or you'll have to format your INSERT to handle the date format properly. Those fields are:
, DOB DATE NOT NULL
, HIRING_DATE DATE NOT NULL
If you chose to keep the columns as DATEs, then in your insert use (for the above-mentioned columns) the following...
,to_date('31-JANUARY-1988','DD-MONTH-YYYY')
,to_date('16-DECEMBER-2013','DD-MONTH-YYYY')
The above won't give you exactly what you are looking for -- e.g. 16 DECEMBER,2013 -- but it will work and format the date with hyphens -- e.g. 16-DECEMBER-2013. If you need further information on the to_date() function for Oracle for formatting and for other sundry information, please refer to the Oracle docs or search on "Oracle TO_DATE" via your favorite browser.
HTH

Oracle external tables

I'm struggling with an Oracle external table, although I researched the Oracle forums. Still, no success.
Let's suppose I have a simple table
DESCRIBE PRODUCTS
Name Null Type
------------------------------ -------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ID NOT NULL NUMBER
NAME VARCHAR2(30)
VALUE NUMBER(5,2)
DEP VARCHAR2(30)
COUNT NUMBER(3)
Then, I created an oracle folder:
CREATE OR REPLACE DIRECTORY ext_prod_dir AS 'c:\';
I save the content of that table in a .lst file
spool c:\products.lst
select p.id || ';' || p.name || ';' || p.value || ';' || p.dep || ';' || p.count FROM products p;
spool off;
P.ID||';'||P.NAME||';'||P.VALUE||';'||P.DEP||';'||P.COUNT
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1;Settlers of Catan;29,95;Toys;3
2;DVD Player;82,97;Electronics;2
3;Red Shirt;12,49;Clothes;3
4;Black Leather Couch;399,99;Furniture;5
5;Oak Cofee Table;223,99;Furniture;5
6;Technodrome;27,99;Toys;4
7;Oh Cereal;3,95;Foods;1
8;Game Console;299,95;Toys;2
9;Video Game;29,95;Toys;3
10;Lawn Chair;34,99;Furniture;11
11;Dog Toy Bone;34,99;Toys;9
12;Heated Blanket;27,95;Toys;8
13;Flux Capacitor;27,95;Toys;7
14;Chocolate Pie;3,14;Foods;7
Then I tried to create the external table:
CREATE TABLE products_ext
(ID NUMBER,
NAME VARCHAR2(30),
VALUE NUMBER(5,2),
DEP VARCHAR2(30),
COUNT NUMBER(3))
ORGANIZATION EXTERNAL
(TYPE oracle_loader DEFAULT DIRECTORY ext_prod_dir
ACCESS PARAMETERS
(RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ';'
MISSING FIELD VALUES ARE NULL
BADFILE ext_prod_dir:'products.bad_xt'
LOGFILE ext_prod_dir:'products.log_xt'
(ID CHAR(6),
NAME CHAR(30),
VALUE CHAR(8),
DEP CHAR(30),
COUNT CHAR(3)))
location ('products.lst')
) REJECT LIMIT UNLIMITED
So far so good. Then when I select data from the external table, I got:
ORA-29913: error in executing ODCIEXTTABLEOPEN callout
ORA-29400: data cartridge error
KUP-00554: error encontered while parsing access parameters
KUP-01005: syntax error: found "badfile": expecting one of: "column (,reject"
KUP-01007: at line 4 column 7
I tried a huge amount of things, but I got variations on this error. Best thing I accomplished was that I got rid of error, but the table was empty. I would be very much indebted If someone with more experience can point me in the right direction.
BADFILE and LOGFILE are not part of the FIELDS clause. So, move them above the FIELDS TERMINATED.
CREATE TABLE products_ext
(ID NUMBER,
NAME VARCHAR2(30),
VALUE NUMBER(5,2),
DEP VARCHAR2(30),
COUNT NUMBER(3))
ORGANIZATION EXTERNAL
(TYPE oracle_loader DEFAULT DIRECTORY ext_prod_dir
ACCESS PARAMETERS
(RECORDS DELIMITED BY NEWLINE
BADFILE ext_prod_dir:'products.bad_xt'
LOGFILE ext_prod_dir:'products.log_xt'
FIELDS TERMINATED BY ';'
MISSING FIELD VALUES ARE NULL
(ID CHAR(6),
NAME CHAR(30),
VALUE CHAR(8),
DEP CHAR(30),
COUNT CHAR(3)))
LOCATION ('products.lst')
) REJECT LIMIT UNLIMITED
Also, you said when you got rid of the error, the table was empty. Did you check the logfile? If the error is with the VALUE column, then check NLS_NUMERIC_CHARACTERS parameter
in view v$nls_parameters.
select * from v$nls_parameters;
Check if the decimal marker is indeed a comma. If not either update this parameter or change it in the data file.

Resources