Oracle database error ORA-00936: missing expression - oracle10g

This query shows error on execution:
SELECT City,Company
FROM invoicelist
WHERE Company='Filmfare'
AND City='Ahmedabad' AND size= 20;
I created this table using this create statement:
create table invoicelist(
"city" varchar(20),
"company" varchar(20),
"month" varchar(20),
"size" varchar(20),
"des" varchar(500)
);

If you quote identifiers with " they become case sensitive.
So either you
select "city" ....
or you create your table without "
create table invoicelist(
city varchar(20),
company varchar(20),
month varchar(20),
size varchar(20),
des varchar(500)
);
BTW, may I recommend you use varchar2 instead of varchar?

Related

When I run this code a syntax error keeps appearing but I am unable to find where it would be

When I run this code, I receive a very vague syntax error: database: syntax error at or near "(". I am unable to find where this syntax error would be.
I have also been told that animal_adoption_history is not an associative entity when it was designed as one.
What have I done wrong when writing it?
The code:
DROP TABLE IF EXISTS customer;
DROP TABLE IF EXISTS animal;
DROP TABLE IF EXISTS animal_adoption_history;
create table customer (
customer_id CHAR(9) NOT NULL,
c_first_name VARCHAR(25),
c_last_name VARCHAR(50),
c_gender CHAR(1),
c_phone_number VARCHAR(20),
c_email_address VARCHAR(45),
c_date_of_birth DATE,
c_address_number VARCHAR(10),
c_street_name VARCHAR(30),
c_city VARCHAR(50),
c_state CHAR(3),
c_postcode CHAR(4),
c_has_adopted_before CHAR(1),
constraint customer_PK PRIMARY KEY (customer_id)
);
create table animal (
animal_id CHAR(9) NOT NULL,
a_animal_type VARCHAR(20) ,
a_breed VARCHAR(50),
a_colour VARCHAR(30),
a_size VARCHAR(20),
a_weight_kg VARCHAR(10),
a_description VARCHAR(75),
a_name VARCHAR(30),
a_date_of_birth DATE,
a_sex CHAR(1),
a_animal_cost INT(10),
a_microchip_status CHAR(1),
a_vaccination_status CHAR(1),
constraint animal_PK PRIMARY KEY (animal_id)
);
create table animal_adoption_history (
health_conditions VARCHAR(100),
is_available_to_adopt CHAR(1),
has_been_adopted_previously CHAR(1),
reason_for_entry VARCHAR(75),
date_entered DATE,
animal_id CHAR(9) NOT NULL,
customer_id CHAR(9) NOT NULL,
constraint animal_adoption_history_PK PRIMARY KEY (animal_id, customer_id),
constraint animal_adoption_history_FK1 FOREIGN KEY (animal) references animal(animal_id)
constraint animal_adoption_history_FK2 FOREIGN KEY (customer_id) references customer(customer_id)
);
INSERT INTO customer (customer_id,c_first_name,c_last_name,c_gender,c_phone_number,c_email_address,c_date_of_birth,c_address_number,c_street_name,c_city,c_state,c_postcode,c_has_adopted_before)
VALUES ('C00000001','Olivia','Smith','F','0422425392','olivia.smith#gmail.com','1980-06-22','2','Henderson Street','Bondi','NSW','2092','Yes');
INSERT INTO customer (customer_id,c_first_name,c_last_name,c_gender,c_phone_number,c_email_address,c_date_of_birth,c_address_number,c_street_name,c_city,c_state,c_postcode,c_has_adopted_before)
VALUES ('C00000002','Taylor','Brown','F','0422435394','taylor.brown#gmail.com','1999-02-24','62','Ultimo Avenue','Bondi','NSW','2092','No');
INSERT INTO customer (customer_id,c_first_name,c_last_name,c_gender,c_phone_number,c_email_address,c_date_of_birth,c_address_number,c_street_name,c_city,c_state,c_postcode,c_has_adopted_before)
VALUES ('C00000003','Sarah','Li','F','0422425342','sarah.li#gmail.com','1997-02-22','27','Winchester Street','Epping','NSW','2092','Yes');
INSERT INTO customer (customer_id,c_first_name,c_last_name,c_gender,c_phone_number,c_email_address,c_date_of_birth,c_address_number,c_street_name,c_city,c_state,c_postcode,c_has_adopted_before)
VALUES ('C00000004','Charlie','Swift','M','0432425392','charlie.swift#gmail.com','1998-02-22','22','Henderson Lane','Lindfield','NSW','2092','No');
INSERT INTO customer (customer_id,c_first_name,c_last_name,c_gender,c_phone_number,c_email_address,c_date_of_birth,c_address_number,c_street_name,c_city,c_state,c_postcode,c_has_adopted_before)
VALUES ('C00000005','Heath','Davidson','M','0422425911','heath.davidson#gmail.com','2003-01-22','22','Station Street','Manly','NSW','2092','Yes');
INSERT INTO animal (animal_id,a_animal_type,a_breed,a_colour,a_size,a_weight_kg,a_description,a_name,a_date_of_birth,a_sex,a_animal_cost,a_microchip_status,a_vaccination_status)
VALUES ('A00000001','Dog','Pug','Light brown','Small','5','Playful yet enjoys cuddles','Mia','2020-02-22','F','3100','Y','Y');
INSERT INTO animal (animal_id,a_animal_type,a_breed,a_colour,a_size,a_weight_kg,a_description,a_name,a_date_of_birth,a_sex,a_animal_cost,a_microchip_status,a_vaccination_status)
VALUES ('A00000002','Cat','Tabby','Orange','Small','4','Quiet and loves the sun','Garfield','2010-04-28','M','1400','Y','N');
INSERT INTO animal (animal_id,a_animal_type,a_breed,a_colour,a_size,a_weight_kg,a_description,a_name,a_date_of_birth,a_sex,a_animal_cost,a_microchip_status,a_vaccination_status)
VALUES ('A00000003','Bird','Budgie','Green and yellow','Extra Small','0.035','Very loud when hungry','Roody','11-14','F','1200','N','Y');
INSERT INTO animal (animal_id,a_animal_type,a_breed,a_colour,a_size,a_weight_kg,a_description,a_name,a_date_of_birth,a_sex,a_animal_cost,a_microchip_status,a_vaccination_status)
VALUES ('A00000004','Rabbit','Holland Lop','Light brown and white','Small','5','Fluffy and enjoys lettuce snacks','Thumper','2018-19-04','F','900','N','N');
INSERT INTO animal (animal_id,a_animal_type,a_breed,a_colour,a_size,a_weight_kg,a_description,a_name,a_date_of_birth,a_sex,a_animal_cost,a_microchip_status,a_vaccination_status)
VALUES ('A00000005','Dog','Golden Retriever','Dark blonde','Large','32','Loves going for long walks','Milo','2014-05-30','M','2500','Y','Y');
INSERT INTO animal_adoption_history (health_conditions,is_available_to_adopt,has_been_adopted_previously,reason_for_entry,date_entered,animal_id,customer_id)
VALUES ('None','Y','Y','Owner moved away','2021-08-18','A00000001','C00000001');
INSERT INTO animal_adoption_history (health_conditions,is_available_to_adopt,has_been_adopted_previously,reason_for_entry,date_entered,animal_id,customer_id)
VALUES ('None','N','N','Newborn looking for home','2022-07-13','A00000003','C00000005');
INSERT INTO animal_adoption_history (health_conditions,is_available_to_adopt,has_been_adopted_previously,reason_for_entry,date_entered,animal_id,customer_id)
VALUES ('Diabetes','Y','Y','Owner passed away','2019-11-01','A00000004','C00000001');
INSERT INTO animal_adoption_history (health_conditions,is_available_to_adopt,has_been_adopted_previously,reason_for_entry,date_entered,animal_id,customer_id)
VALUES ('None','Y','N','Previous household abuse','2014-09-19','A00000002','C00000004');
INSERT INTO animal_adoption_history (health_conditions,is_available_to_adopt,has_been_adopted_previously,reason_for_entry,date_entered,animal_id,customer_id)
VALUES ('Arthritis','Y','Y','Newborn looking for home','2016-04-26','A00000005','C00000002');
Thanks!
INT(10) is not valid datatype:
a_animal_cost INT(10)
There are other issues as well, you have to fix this.
You have a number of errors in your code. When running PostgreSQL only lists the first one, which in this case is the line a_animal_cost INT(10),. Integers are a fixed size so it is wrong to attempt to specify a size for it.
Also do not use char(1) for Yes ('Y') No ('N') fields. PostgreSQL has a native boolean data type for this purpose, so use it.
When deleting tables, you need to delete the lowest tables in the hierarchy first (foreign key etc).
Finally, you can chain multiple inserts as values together.
Putting all this together, I would recommend that you use something like this:
DROP TABLE IF EXISTS animal_adoption_history;
DROP TABLE IF EXISTS animal;
DROP TABLE IF EXISTS customer;
create table customer (
customer_id CHAR(9) NOT NULL,
c_first_name VARCHAR(25),
c_last_name VARCHAR(50),
c_gender CHAR(1),
c_phone_number VARCHAR(20),
c_email_address VARCHAR(45),
c_date_of_birth DATE,
c_address_number VARCHAR(10),
c_street_name VARCHAR(30),
c_city VARCHAR(50),
c_state CHAR(3),
c_postcode CHAR(4),
c_has_adopted_before boolean,
constraint customer_PK PRIMARY KEY (customer_id)
);
create table animal (
animal_id CHAR(9) NOT NULL,
a_animal_type VARCHAR(20) ,
a_breed VARCHAR(50),
a_colour VARCHAR(30),
a_size VARCHAR(20),
a_weight_kg VARCHAR(10),
a_description VARCHAR(75),
a_name VARCHAR(30),
a_date_of_birth DATE,
a_sex CHAR(1),
a_animal_cost INT,
a_microchip_status boolean,
a_vaccination_status boolean,
constraint animal_PK PRIMARY KEY (animal_id)
);
create table animal_adoption_history (
health_conditions VARCHAR(100),
is_available_to_adopt boolean,
has_been_adopted_previously boolean,
reason_for_entry VARCHAR(75),
date_entered DATE,
animal_id CHAR(9) NOT NULL,
customer_id CHAR(9) NOT NULL,
constraint animal_adoption_history_PK PRIMARY KEY (animal_id, customer_id),
constraint animal_adoption_history_FK1 FOREIGN KEY (animal_id) references animal(animal_id),
constraint animal_adoption_history_FK2 FOREIGN KEY (customer_id) references customer(customer_id)
);
INSERT INTO customer (customer_id,c_first_name,c_last_name,c_gender,c_phone_number,c_email_address,c_date_of_birth,c_address_number,
c_street_name,c_city,c_state,c_postcode,c_has_adopted_before) VALUES
('C00000001','Olivia','Smith','F','0422425392','olivia.smith#gmail.com','1980-06-22','2',
'Henderson Street','Bondi','NSW','2092',true),
('C00000002','Taylor','Brown','F','0422435394','taylor.brown#gmail.com','1999-02-24','62',
'Ultimo Avenue','Bondi','NSW','2092',false),
('C00000003','Sarah','Li','F','0422425342','sarah.li#gmail.com','1997-02-22','27',
'Winchester Street','Epping','NSW','2092',true),
('C00000004','Charlie','Swift','M','0432425392','charlie.swift#gmail.com','1998-02-22','22',
'Henderson Lane','Lindfield','NSW','2092',false),
('C00000005','Heath','Davidson','M','0422425911','heath.davidson#gmail.com','2003-01-22','22',
'Station Street','Manly','NSW','2092',true);
INSERT INTO animal (animal_id,a_animal_type,a_breed,a_colour,a_size,a_weight_kg,a_description,a_name,a_date_of_birth,
a_sex,a_animal_cost,a_microchip_status,a_vaccination_status) VALUES
('A00000001','Dog','Pug','Light brown','Small','5','Playful yet enjoys cuddles','Mia','2020-02-22',
'F','3100',true,true),
('A00000002','Cat','Tabby','Orange','Small','4','Quiet and loves the sun','Garfield','2010-04-28',
'M','1400',true,false),
('A00000003','Bird','Budgie','Green and yellow','Extra Small','0.035','Very loud when hungry','Roody','2020-11-14',
'F','1200',false,true),
('A00000004','Rabbit','Holland Lop','Light brown and white','Small','5','Fluffy and enjoys lettuce snacks','Thumper','2018-04-19',
'F','900',false,false),
('A00000005','Dog','Golden Retriever','Dark blonde','Large','32','Loves going for long walks','Milo','2014-05-30',
'M','2500',true,true);
INSERT INTO animal_adoption_history (health_conditions,is_available_to_adopt,has_been_adopted_previously,reason_for_entry,
date_entered,animal_id,customer_id) VALUES
('None',true,true,'Owner moved away','2021-08-18','A00000001','C00000001'),
('None',false,false,'Newborn looking for home','2022-07-13','A00000003','C00000005'),
('Diabetes',true,true,'Owner passed away','2019-11-01','A00000004','C00000001'),
('None',true,false,'Previous household abuse','2014-09-19','A00000002','C00000004'),
('Arthritis',true,true,'Newborn looking for home','2016-04-26','A00000005','C00000002');

CREATE TABLE DB2 SQLSTATE: 42601, SQLCODE: -104): DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601

I am trying to create a DB2 table with the following
CREATE TABLE ACQ_FAH_DEV.fah_balance_ledger
(
"ACTIVE" VARCHAR(10),
INPUT_BY VARCHAR(32),
INPUT_TIME DATE,
AMENDED_BY VARCHAR(32),
AMENDED_TIME DATE,
ENTITY VARCHAR(20),
ACCOUNT_CODE VARCHAR(20),
ACCOUNT_NAME VARCHAR(255),
PORTFOLIO_CODE VARCHAR(100),
OM_LOAD_RUN_ID VARCHAR(128) NOT NULL,    
OM_LOAD_TMST TIMESTAMP(6) NOT NULL,    
BM_BUSINESS_INTERVAL_TYP VARCHAR(20) NOT NULL,
BM_BUSINESS_INTERVAL_TMST TIMESTAMP(6) NOT NULL,    
BM_BUSINESS_INTERVAL_START_END_FLAG VARCHAR(10) NOT NULL,    
SM_SOURCE_SYSTEM_CD VARCHAR(16) NOT NULL,    
OM_UNIQUE_ROW_ID BIGINT NOT NULL,    
OM_USER_ID VARCHAR(100) NOT NULL,    
OM_VERSION_ID SMALLINT NOT NULL
)
ORGANIZE BY COLUMN IN ACQ_FAH_DEV
DISTRIBUTE BY HASH(
ACCOUNT_CODE,
OM_VERSION_ID,
BM_BUSINESS_INTERVAL_TYP
);
but running into this error
(SQLSTATE: 42601, SQLCODE: -104): DB2 SQL Error: SQLCODE=-104,
SQLSTATE=42601, SQLERRMC=(;LOAD_RUN_ID VARCHAR;BINARY, DRIVER=4.26.14
SQLSTATE 42601: A character, token, or clause is invalid or missing.
SQL0104N An unexpected token "(" was found following "LOAD_RUN_ID
VARCHAR". Expected tokens may include: "BINARY".
The error seems innocuous but I am not able to see why this is failing.
For LUW, the tablespace clause should be before the organized clause (I assume IN ACQ_FAH_DEV refers to which tablespace to put the table in). Try:
CREATE TABLE ACQ_FAH_DEV.fah_balance_ledger
(
"ACTIVE" VARCHAR(10),
INPUT_BY VARCHAR(32),
INPUT_TIME DATE,
AMENDED_BY VARCHAR(32),
AMENDED_TIME DATE,
ENTITY VARCHAR(20),
ACCOUNT_CODE VARCHAR(20),
ACCOUNT_NAME VARCHAR(255),
PORTFOLIO_CODE VARCHAR(100),
OM_LOAD_RUN_ID VARCHAR(128) NOT NULL,
OM_LOAD_TMST TIMESTAMP(6) NOT NULL,
BM_BUSINESS_INTERVAL_TYP VARCHAR(20) NOT NULL,
BM_BUSINESS_INTERVAL_TMST TIMESTAMP(6) NOT NULL,
BM_BUSINESS_INTERVAL_START_END_FLAG VARCHAR(10) NOT NULL,
SM_SOURCE_SYSTEM_CD VARCHAR(16) NOT NULL,
OM_UNIQUE_ROW_ID BIGINT NOT NULL,
OM_USER_ID VARCHAR(100) NOT NULL,
OM_VERSION_ID SMALLINT NOT NULL
)
IN ACQ_FAH_DEV
ORGANIZE BY COLUMN
DISTRIBUTE BY HASH(
ACCOUNT_CODE,
OM_VERSION_ID,
BM_BUSINESS_INTERVAL_TYP
);
Documentation for CREATE TABLE statement

PostgreSQL Insert with failed unique constraint

CREATE TABLE s_etpta.sfphierg (
hierar VARCHAR(10) NOT NULL,
libelle VARCHAR(40),
typfct VARCHAR(1),
utilcre VARCHAR(10),
datcre DATE,
utilmod VARCHAR(10),
datmod DATE,
CONSTRAINT i_sfphierg PRIMARY KEY(hierar)
)
CREATE TABLE s_etpta.hopsech (
horsect VARCHAR(40) NOT NULL,
libelle VARCHAR(40),
libcourt VARCHAR(20),
horcode VARCHAR(10),
CONSTRAINT i_hopsech PRIMARY KEY(horsect)
)
BEGIN
delete from SFPHIERG;
INSERT INTO SFPHIERG ("hierar", "libelle", "typfct", "utilcre", "datcre",
"utilmod", "datmod")
select '01'||horcode, E'Hircuit standard'||horcode, E'1', E'HQS', E'2007-01-
29', E' ', E'1900-01-01'
FROM HOPSECH where HOPSECH.IJIGHSUPPM like '1'
and not exists (select hierar from SFPHIERG where hierar like '01'||horcode);
INSERT INTO SFPHIERG ("hierar", "libelle", "typfct", "utilcre", "datcre",
"utilmod", "datmod")
select '00'||horcode, E'Circuit cascade'||horcode, E'1', E'HQS', E'2007-01-
29', E' ', E'1900-01-01'
FROM HOPSECH where HOPSECH.IJIGHSUPPR like '1'
and not exists (select hierar from SFPHIERG where hierar like '00'||horcode);
END;
In my functions's body I have two insert queries executed after a delete query.
The rows are correctly inserted when my first column doesn't exist, when it does I have the failed unique constraint error, when this error occur nothing is added into my table.
Is there a way I can stop this error from blocking all the inserts and only the inserts where my first column exists?

Why select statement inside stored procedure in PostgreSQL throws error 'column does not exist..?

I have this stored procedure in PostgreSQL where I want to make an insert in a table. I get some parameters from the procedure and using them I have tried to select other attributes on other tables.
This is my stored procedure:
CREATE OR REPLACE FUNCTION "public"."prc_sales_invoice_header_insert"("customercode" varchar, "sales_note" varchar, "automatic_payment_id" int4, "cash_register_code" varchar,...etc)
RETURNS "pg_catalog"."void" AS $BODY$
--declaring variables to store data from the tables
DECLARE
salesdate date;
salesdocumentserial varchar;
currencycode varchar;
currencycode2 varchar;
customername varchar;
warehousecode varchar;
......etc.
BEGIN
--getting values from tables and storing to variables
SELECT "name" into customername from public."customers" where "customer_code" = customercode;
SELECT CURRENT_DATE into salesdate;
SELECT max(sales_invoice_header_id) into salesdocumentserial from public."sales_invoice_header";
.....
--inserting values
INSERT INTO public."sales_invoice_header"("sales_date",
"sales_document_serial",
"currency_code",
"currency_code2",
"customer_code",
....
VALUES(
salesdate,
salesdocumentserial,
currencycode,
currencycode2,
customer_code,
.....)
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
When I try to execute it throws an error saying:
"ERROR: column "customer_code" does not exist", "HINT: There is a column named "customer_code" in table "sales_invoice_header", but it cannot be referenced from this part of the query."
Table customers exists and there is a column named costomer_code but I don't understand why it cannot reference it.
Table customers:
-- ----------------------------
-- Table structure for customers
-- ----------------------------
DROP TABLE IF EXISTS "public"."customers";
CREATE TABLE "public"."customers" (
"customer_id" int4 NOT NULL DEFAULT nextval('"Customers_CustomerId_seq"'::regclass),
"customer_code" varchar COLLATE "pg_catalog"."default",
"barcode" varchar COLLATE "pg_catalog"."default",
"qr_code" varchar COLLATE "pg_catalog"."default",
"tax_id" varchar COLLATE "pg_catalog"."default",
"business_id" varchar COLLATE "pg_catalog"."default",
"city_id" int8,
"mobile" varchar COLLATE "pg_catalog"."default",
"accounting_number" varchar COLLATE "pg_catalog"."default",
"name" varchar COLLATE "pg_catalog"."default"
);
Can anyone help me with this, what I am doing wrong? Or is this the correct way of doing things?
Thanks in advance.
INSERT INTO public."sales_invoice_header"("sales_date",
"sales_document_serial",
"currency_code",
"currency_code2",
"customer_code",
....
VALUES(
salesdate,
salesdocumentserial,
currencycode,
currencycode2,
customer_code,
here customer_code is not a variable, nor literal - it is a column name. yet you don't select it form table - you try to use it in VALUES - won't work. either use
insert into ... select ...,customer_code from sales_invoice_header
or
insert into ... values(..., VAR_customer_code)
or
insert into ... values(..., 'value_customer_code')
edit:
as a_horse_with_no_name noticed my VAR_customer_code, must be your customercode first argument?..

ORA-00913: too many values Error (Even though I have checked my table and entries)

Here's is the table :
create table Student(
student_id char(6) primary key,
name varchar(10) not null,
department char(20),
grade char(6),
percentage smallint,
contact_no char(12),
address varchar(50)
);
This is what I'm inserting :
insert into Student
values ('ST-01','Zain','coe','A+',74,'9999777865','New Delhi');
Why ORA-00913: too many values error when I'm not inserting extra values ?