I'm trying to write the SQL to copy a row from one table to another but I keep getting an invalid identifier in the WHERE clause. I'm using oracle apex.
Here is my code:
INSERT INTO CRIMECLOSED (crimeClosedID, crimeName, crimeDate, crimeNotes,
outsideSourceDescription, dateClosed, relatedCrimes,
staffID, crimeTypeID, locationID)
SELECT CRIMEOPEN.crimeOpenID, CRIMEOPEN.crimeName, CRIMEOPEN.crimeDate,
CRIMEOPEN.crimeNotes, CRIMEOPEN.outsideSourceDescription, CURDATE(),
CRIMEOPEN.relatedCrimes, CRIMEOPEN.staffID, CRIMEOPEN.crimeTypeID,
CRIMEOPEN.locationID
FROM CRIMEOPEN
WHERE CRIMEOPEN.crimeOpenID = '1';
CRIMEOPEN table
CREATE TABLE "CRIMEOPEN"
( "crimeOpenID" VARCHAR2(5),
"crimeName" VARCHAR2(20),
"crimeDate" DATE,
"crimeNotes" VARCHAR2(200),
"outsideSourceDescription" VARCHAR2(200),
"relatedCrimes" VARCHAR2(5),
"staffID" VARCHAR2(5),
"crimeTypeID" VARCHAR2(5),
"locationID" VARCHAR2(5),
CONSTRAINT "CRIMEOPEN_PK" PRIMARY KEY ("crimeOpenID") ENABLE
)
The error I get is:
ORA-00904: "CRIMEOPEN"."CRIMEOPENID": invalid identifier
I think the error is trying to say that 'crimeOpenID' is not a column in 'CRIMEOPEN' but it is.
Any help please?
If you use double quotes when declaring the columns of the table then the same format needs to be used also when "using" the columns in various statements.
So in order to fix the issue you should wrap all columns in double quotes in the select statement (something like below - applied only to the select statement).
INSERT INTO CRIMECLOSED (crimeClosedID, crimeName, crimeDate, crimeNotes,
outsideSourceDescription, dateClosed, relatedCrimes,
staffID, crimeTypeID, locationID)
SELECT "CRIMEOPEN"."crimeOpenID", "CRIMEOPEN"."crimeName", "CRIMEOPEN"."crimeDate",
"CRIMEOPEN"."crimeNotes", "CRIMEOPEN"."outsideSourceDescription", CURDATE(),
"CRIMEOPEN"."relatedCrimes", "CRIMEOPEN"."staffID", "CRIMEOPEN"."crimeTypeID",
"CRIMEOPEN"."locationID"
FROM "CRIMEOPEN"
WHERE "CRIMEOPEN"."crimeOpenID" = '1';
Related
Code below using sample data.
INSERT INTO ClientSeller VALUES
(1,'John Smith',88,1,'a',1),
(2,'Joe Smith',12,2,'b',2),
(3,'Warren ',15,2,'c',3),
(4,'Karen',69,6,'d',5),
(5,'Bob',45,6,'e',55),
(6,'Owen',65,6,'f',4),
(7,'Steve',25,5,'g',8),
(8,'Peter',24,55,'a',88),
(9,'Zoe',245,8,'b',8),
(10,'Jacky',244,2,'c',8);
and displays :
ORA-00933: SQL command not properly ended
Can any explain why this does not execute?
Based on your example, I created the following table.
Create table ClientSeller (
identity number(2),
name varchar2(50),
employeno number(2),
otherno number(1),
letter char(1),
otherotherno number(1)
)
Then using the code below I can insert two of your sample rows into the table. Oracle has a very clunky syntax for inserting values into a table. You absolutely need the Insert All with separate INTO tablename VALUES xxxxx for each values set and then at the end you MUST add the select 1 from DUAL. See this example for more details.
Also, do not end your statements with the semicolon. In none of the example code provided here will you find such a character.
INSERT ALL
INTO ClientSeller VALUES (1,'John Smith',88,1,'a',1)
INTO ClientSeller VALUES (2,'Joe Smith',12,2,'b',2)
SELECT 1 from DUAL
Can somebody correct me if I'm doing any syntax mistake while inserting into a table with complex data type?
This is my code:
CREATE TYPE ADDR AS OBJECT (
street VARCHAR2(32),
city VARCHAR2(32),
zip NUMBER(32)
);
CREATE TYPE NAMES AS OBJECT(
firstname VARCHAR2(64),
lastname VARCHAR2(64)
);
CREATE TABLE PERSON(
pid NUMBER(10),
pname NAMES,
paddr ADDR,
pbirthdate DATE
);
INSERT INTO PERSON
VALUES (1, pname('John','Doe'), paddr('None','Test',51050), TO_DATE('33445555', 'MMDDYYY'));
When you initialize an instance of an object type, you need to specify the type name, not the column name. You should also always specify the target columns in an INSERT statement.
INSERT INTO PERSON
(pid, pname, paddr, pbirthdate)
VALUES
(1, names('John','Doe'), addr('None','Test',51050), TO_DATE('33445555', 'MMDDYYY'));
^ ^
| here | here
Additionally, 33445555 is an invalid date given the format mask MMDDYYY'
The error message clearly points to this:
TO_DATE('33445555', 'MMDDYYY')
When I try to use this in a query while testing locally, I get this error message:
ORA-01843: not a valid month
The date you specified is not valid, because there is no 33rd month. Try using a valid date, and the error should go away:
TO_DATE('06242019', 'MMDDYYYY')
There is issue in TYPE usage and date:
INSERT INTO PERSON
VALUES (1, NAMES('John','Doe'), ADDR('None','Test',51050), TO_DATE('06242019', 'MMDDYYYY'));
Db Fiddle Demo
Cheers!!
So I wrote a query pretty much the same as the one described in this answer. However I'm getting an error stating that: No such column b.when.
My select statement:
SELECT
mileage,
(SELECT b.mileage FROM MileageEvents as b WHERE `b.when` < `a.when` ORDER BY `b.when` DESC LIMIT 1) as last_mileage,
gallons,
cost_per_gallon,
`when`
FROM MileageEvents as a
I know I've written such queries previously, but I can't seem to figure out what is going on with this query. What am I doing wrong?
A dump of my database:
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "MileageEvents" (
"mileage" INTEGER,
"when" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"cost_per_gallon" INTEGER,
"gallons" INTEGER,
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"car_id" INTEGER NOT NULL
);
INSERT INTO MileageEvents VALUES(150000,'2019-03-12',3.3500000000000000888,12,1,1);
INSERT INTO MileageEvents VALUES(150300,'2019-03-19',3.25,12,2,1);
INSERT INTO MileageEvents VALUES(150693,'2019-03-22',3.4500000000000001776,12,3,1);
INSERT INTO MileageEvents VALUES(151000,'2019-03-25',3.3900000000000001243,12,4,1);
INSERT INTO MileageEvents VALUES(151600,'2019-04-01',2.25,12,5,1);
INSERT INTO MileageEvents VALUES(151883,'2019-06-10 23:01:43',2.4500000000000001776,11.695999999999999729,6,1);
DELETE FROM sqlite_sequence;
INSERT INTO sqlite_sequence VALUES('MileageEvents',6);
COMMIT;
(First, I'd use normal double quote escaping instead of MySQL style backticks, or better yet, an identifier that's not a reserved keyword)
You need to use b."when". What you're doing treats the entire string b.when as an identifier, with no split between table name and column name.
Rewritten:
SELECT
mileage,
(SELECT b.mileage FROM MileageEvents as b WHERE b."when" < a."when" ORDER BY b."when" DESC LIMIT 1) as last_mileage,
gallons,
cost_per_gallon,
"when"
FROM MileageEvents as a;
Note: if using sqlite 3.25 or newer, you can avoid the subquery completely:
SELECT
mileage,
lag(mileage) OVER (ORDER BY "when") AS last_mileage,
gallons,
cost_per_gallon,
"when"
FROM MileageEvents;
proc sql;
connect to teradata as tera(mode=teradata server=oneview user="&teraid." password="&terapwd.");
execute(CREATE MULTISET TABLE UD497.PAN_AM_EMAIL
(
ATHNUM DECIMAL(10,0),
BLK_1_CDE CHAR(1),
BLK_2_CDE CHAR(1),
OPEN_DT DATE,
LANGUAGE CHAR(7),
MKTCELL CHAR(2),
PROJECT_ID CHAR(15),
CAMPAIGN CHAR(35);
) PRIMARY INDEX(ATHNUM);
) by tera;
Error Message :
ERROR: Teradata execute: Syntax error, expected something like a 'CHECK' keyword between ',' and the 'LANGUAGE' keyword.
The error message suggests that LANGUAGE is a keyword in Teradata, simply double quote it:
"LANGUAGE" CHAR(7),
But now, whenever you use it in SQL, you must double quote it, e.g.
select "LANGUAGE"...
Maybe simply change the name.
I have created table using this command successfully
create table Person(
first_name varchar(25) not null,
last_name varchar(25) not null,
persoin_id number not null,
birth_date date,
country varchar (25),
salary number);
and now I want to insert data into that table
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
values(100,'dato','datuashvili',350,to_date('01/01/10','DD/MM/YY'),'georgia');
values(101,'irakli','oqruashvili',350,to_date('01/03/10','DD/MM/YY'),'georgia');
first row is inserted,but problem is with second line
1 rows inserted.
Error starting at line 10 in command:
values(101,'irakli','oqruashvili',350,to_date('01/03/10','DD/MM/YY'),'georgia')
Error report:
Unknown Command
Please help me to determine what is problem?thanks
If you are on a RDBMS that supports multi-rows inserts in one INSERT:
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
values
(100,'dato','datuashvili',350,to_date('01/01/10','DD/MM/YY'),'georgia') ,
--- comma here ---^
(101,'irakli','oqruashvili',350,to_date('01/03/10','DD/MM/YY'),'georgia') ;
^--- no "values" here
If not (like Oracle), you'll have to issue two insert statements:
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
values
(100,'dato','datuashvili',350,to_date('01/01/10','DD/MM/YY'),'georgia') ;
--- as it was here ---^
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
values
(101,'irakli','oqruashvili',350,to_date('01/03/10','DD/MM/YY'),'georgia') ;
or use this approach:
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
select
(100,'dato','datuashvili',350,to_date('01/01/10','DD/MM/YY'),'georgia')
from dual
union all select
(101,'irakli','oqruashvili',350,to_date('01/03/10','DD/MM/YY'),'georgia')
from dual
;
You will need to use 2 insert statement instead of one for 2 different sets of data...
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
values(100,'dato','datuashvili',350,to_date('01/01/10','DD/MM/YY'),'georgia');
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
values(101,'irakli','oqruashvili',350,to_date('01/03/10','DD/MM/YY'),'georgia')
You have a ; at the end of:
values(100,'dato','datuashvili',350,to_date('01/01/10','DD/MM/YY'),'georgia');
^
change it to , and also loose the values from the next line. You need just one values per insert.