Missing comma in SQL while insert - sql

Code:
CREATE TABLE PERSONNE1 (
NUMP NUMBER,
PRENOM VARCHAR2(25),
NOM VARCHAR2(25),
DATENAIS DATE,
CONSTRAINT PK_NUMP PRIMARY KEY (NUMP)
);
create sequence numpersonne
start with 0
increment by 2
minvalue -1;
SQL> insert into PERSONNE1 (numpersonne.nextval, "Jack", "Nicholson", to_date('04/22/1937','dd/mm/yyyy'));
insert into PERSONNE1 (numpersonne.nextval, "Jack", "Nicholson", to_date('04/22/1937','dd/mm/yyyy'))
Error:
ERROR at line 1:
ORA-00917: missing comma

String literals in SQL are denoted by single quotes ('), not double quotes (").
More importantly, as noted in the comments, you're missing the values keyword:
insert into PERSONNE1
values -- was missing
(numpersonne.nextval,
'Jack', -- Fix the quotes here
'Nicholson', -- And here
to_date('04/22/1937','mm/dd/yyyy'));
Edit:
The date format was also wrong - you need to switch dd and mm.

The immediate cause of the error you're seeing is that you've got your values in the wrong place; at the moment they're (sort of) part of the column list, and you can't have a function call inside that - so the opening parenthesis is being flagged. And that's because you're missing the values keyword.
As #Mureinik said, the string enclosure is a single quote, not a double quote; and you also have your day and month the wrong way round:
insert into PERSONNE1
values (numpersonne.nextval, 'Jack', 'Nicholson', to_date('04/22/1937','mm/dd/yyyy'));
or using a date literal to avoid that potential for confusion:
insert into PERSONNE1
values (numpersonne.nextval, 'Jack', 'Nicholson', date '1937-04-22');
It is also a good idea to explicitly list the columns you're inserting into:
insert into PERSONNE1 (NUMP, PRENOM, NOM, DATENAIS)
values (numpersonne.nextval, 'Jack', 'Nicholson', date '1937-04-22');

Related

ORA-01722: invalid number when inserting decimal number in single quotes with [duplicate]

This question already has answers here:
NLS_NUMERIC_CHARACTERS setting for decimal
(4 answers)
Closed 3 months ago.
I'm getting error ORA-01722: invalid number with following query:
insert into foo (id, some_number) values('id_01', '8.9');
What I've found so far:
I can run the query if I remove single quotes, like:
insert into foo (id, some_number) values('id_01', 8.9);
Or if I change . to , like:
insert into foo (id, some_number) values('id_01', '8,9');
I'm using Oracle database.
Funny thing: I'm located in Spain, where decimal numbers are written with , instead of .
Another developer in my team, not located in Spain can insert those values with the first query (with single quotes and .) which leads me to think this might be due to some system properties. My computer language is English, macOS Monterey 12.5.1
TABLE: FOO
Columns
NAME DATA TYPE NULL DEFAULT COMMENTS
*ID VARCHAR2(20 BYTE) No
SOME_NUMBER NUMBER(3,1) Yes
Looks like issue with national language support, i.e. NLS_NUMERIC_CHARACTERS.
SQL> create table foo (id varchar2(20), some_number number(3, 1));
Table created.
SQL> insert into foo(id, some_number) values ('id_01', '8.9');
insert into foo(id, some_number) values ('id_01', '8.9')
*
ERROR at line 1:
ORA-01722: invalid number
Failed (as you already know). If nls_numeric_characters is set to accept
dot as a decimal character and
comma as a group separator,
then it works:
SQL> alter session set nls_numeric_characters = '.,';
Session altered.
SQL> insert into foo(id, some_number) values ('id_01', '8.9');
1 row created.
SQL>

data type in Oracle

how to resolve this?
INSERT INTO logiciel VALUES ('log1','Oracle 6',13-05-1995,'6.2','UNIX','3000');
INSERT INTO logiciel VALUES ('log1','Oracle 6',13-05-1995,'6.2','UNIX','3000')
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
the error
here's the table
the table
create table logiciel (
nLog varchar2 (5) primary key,
nomLog varchar (20) not null,
dateAchat date,
version varchar2 (7),
typeLog varchar2 (9),
prix number (6,2)
);
Use date literal or TO_DATE function with appropriate format mask. Do not insert strings into date datatype columns, hoping that Oracle will guess format you used.
SQL> -- date literal is always DATE 'yyyy-mm-dd'
SQL> INSERT INTO logiciel (nlog, nomlog, dateachat, version, typelog, prix)
2 VALUES ('log1', 'Oracle 6', date '1995-05-13', '6.2', 'UNIX', '3000');
1 row created.
SQL> -- TO_DATE lets you choose format, but then the format mask must follow it
SQL> INSERT INTO logiciel (nlog, nomlog, dateachat, version, typelog, prix)
2 VALUES ('log2', 'Oracle 6', to_date('13-05-1995', 'dd-mm-yyyy'), '6.2', 'UNIX', '3000');
1 row created.
SQL>
You have to use quotes around your date value as dates are treated as string -
INSERT INTO logiciel VALUES ('log1','Oracle 6','13-05-1995','6.2','UNIX','3000');
But only using string will tell the DB to store it as string not as date. The difference between them is, You can not do any date manipulation on strings. So you have 2 option now.
Use To_Date function with date format -
INSERT INTO logiciel VALUES ('log1','Oracle 6',TO_DATE('13-05-1995', 'DD-MM-YYYY'),'6.2','UNIX','3000');
Use DATE keyword which is supported by ANSI standard but with that, you have to use the date format as 'YYYY-MM-DD'-
INSERT INTO logiciel VALUES ('log1','Oracle 6',DATE '1995-05-13','6.2','UNIX','3000');

Can I not use a function in an insert statement?

It seems as though I cannot use a SAS function in an insert statement:
proc sql;
create table tq84_tab (col char(100));
insert into tq84_tab values (repeat('foo ', 10));
quit;
When I run the code, I am getting:
insert into tq84_tab values (repeat('foo ', 10));
---- -----
22 26
202 200
ERROR 22-322: Syntax error, expecting one of the following: a quoted string, a numeric constant, a datetime constant,
a missing value, +, -, MISSING, NULL, USER.
ERROR 200-322: The symbol is not recognized and will be ignored.
ERROR 202-322: The option or parameter is not recognized and will be ignored.
Am I doing something wrong or is my suspiscion indeed the case?
For an item to appear N times you repeat it N-1 times. You'll also need to macro quote the item if you want it to repeat a trailing space:
insert into tq84_tab values ("%sysfunc(repeat(%str(foo ), 9))");
You can also create a dummy table with only one row. Then use run-time functions to construct the value to insert.
create table onerow (ignore_me char(0));
insert into onerow values ('');
insert into tq84_tab select (repeat("foo ",9)) as col from onerow;
onerow is acting as Oracle's DUAL or SQL Server's bare select (no from).
No/Maybe.
With insert .. values you have to you fixed values, no functions.
In this simple case you'd be able to help yourself with SAS macro - call the SAS function using %sysfunc macro function, that "pre-processes" your code.
proc sql;
create table tq84_tab (col char(100));
insert into tq84_tab values ("%sysfunc(repeat(foo, 10))");
select * from tq84_tab;
quit;

Making columns optional in Oracle

I'm having a bit of an Oracle dilemma.
I have a table consisting of 5 columns: one PK, two FKs, one INT value and one Date.
I also have a sequence set up for my PK.
I set up a trigger that replaces the PK by an auto-incremented value and the Date by the current date so that you can enter the values (null, FK, FK, INT, null) but I was wondering if there was a way to modify my trigger so that I can enter simply (FK, FK, INT)? As it stands (obviously) if I enter only 3 values I get the ORA-00947: not enough values error.
CREATE or REPLACE trigger TRIG_new_product
before insert on product
for each row
BEGIN
SELECT sq_product.nextval, sysdate
into :new.productID, :new.productDate
FROM dual;
END TRIG_new_product;
If you don't want to supply values for a column, don't list it in the insert statement:
insert into product
(fk_column, fk_column, int_column)
values
(42, 24, 4224);
The error message "not enough" values has nothing to do with your trigger and probably stems from the fact that you didn't specify the columns in your insert statement. In that case you have to supply a value for each column. You probably did something like this:
insert into product -- no columns specified therefore all are required
values
(42, 24, 4224);
Of course leaving out columns during insert will only work if they are defined as nullable.
Btw: your trigger could be written a bit simpler:
CREATE or REPLACE trigger TRIG_new_product
before insert on product
for each row
BEGIN
:new.productID := sq_product.nextval;
:new.productDate := sysdate;
END TRIG_new_product;

ORA-00984 column not allowed here

I am getting error
"Execute-984 ORA-00984: column not allowed here"
while I am inserting values in my table Registred_Customer using Pro*C
Registred_Customer is defined as
CREATE TABLE Registred_Customer (
Cust_id NUMBER(6) PRIMARY KEY,
Name VARCHAR2(20) NOT NULL,
Age NUMBER,
Sex CHAR,
Addr VARCHAR2(50),
Contact NUMBER(10)
);
Inserting values using a pro*c method
addCustomer(i, name,age, gender, address,contectNo);
in Pro*C method I use following code to insert
EXEC SQL INSERT INTO REGISTRED_CUSTOMER VALUES
(cust_id, cust_name, age, sex, addr, contact);
here cust_name and addr are char *; and sex is char rest as int;
It reports error while using variable but works fine using direct values
like EXEC SQL INSERT INTO REGISTRED_CUSTOMER VALUES (10, 'Pankaj', 23, 'M', 'asdfs', 45875);
I tried changing few lines but in vain.
Thanks in advance.
Your Pro*C code is basically missing the colons (assuming that your formal parameters are called cust_id, cust_name, age etc.):
EXEC SQL INSERT INTO REGISTRED_CUSTOMER VALUES
(:cust_id, :cust_name, :age, :sex, :addr, :contact);
And it would be more robust to explicitly specify the columns name. Otherwise a change to the table schema can result in difficult to find bugs:
EXEC SQL INSERT INTO REGISTRED_CUSTOMER (Cust_Id, Name, Ag, Sex, Addr, Contact)
VALUES (:cust_id, :cust_name, :age, :sex, :addr, :contact);
If im seeing correct you are trying to insert into the columns, the columns??
"EXEC SQL INSERT INTO REGISTRED_CUSTOMER VALUES (cust_id, cust_name, age, sex, addr, contact);"??
it would be more helpful if you post your procedure complete.
Regards
As Mr. mentioned, you are trying to use the columns as input values. When you provide actual values it works. Are you perhaps meaning to use PL/SQL variables or the procedure arguments? In this case, whatever your procedure parameters are called is what you should put in the values section.
i.e if addCustomer looks like
PROCEDURE addCustomer (pId NUMBER, pName VARCHAR2, pAge NUMBER, pGender CHAR, pAddress VARCHAR2, pContact NUMBER)
Then you'd do something like
INSERT INTO registered_customer (cust_id, name, age, sex, addr, contact) VALUES (pId, pName, pAge, pGender, pAddress, pContact);
But if you are inserting into all columns you can leave out the column definition and just provide values
I also got this error message in a stored procedure doing an insert. I misspelled a parameter name in the values clause and the oracle interpreter saw the misspelled name as a column name and issued the 00984.