i got trouble when insert data using this query :
INSERT ALL
INTO obat ('id_obat','nama_obat','tanggal_kadarluarsa','stock','harga')
VALUES (1, 'Indomethacin', '2023-09-01', 50, 3000)
SELECT * FROM dual;
this is the table query :
CREATE TABLE obat (
id_obat INTEGER NOT NULL,
nama_obat VARCHAR2(255) NOT NULL,
tanggal_kadarluarsa DATE NOT NULL,
stock INTEGER NOT NULL,
harga NUMBER(20, 2) NOT NULL,
CONSTRAINT obat_pk PRIMARY KEY ( id_obat )
);
is somethin wrong with my code?
Single quotes are for string literals; identifiers (e.g. column names) should be in double quotes - but only have to be if they were created as quoted identifiers, and then the case of the name has to match the data dictionary name exactly. Yours are not quoted identifiers in the create statement, so you can just do:
INSERT ALL
INTO obat (id_obat,nama_obat,tanggal_kadarluarsa,stock,harga)
VALUES (1, 'Indomethacin', DATE '2023-09-01', 50, 3000)
SELECT * FROM dual;
If you really wanted to quote them then you would need to do (including the table name to demonstrate, as the same rules apply):
INSERT ALL
INTO "OBAT" ("ID_OBAT","NAMA_OBAT","TANGGAL_KADARLUARSA","STOCK,HARGA")
VALUES (1, 'Indomethacin', DATE '2023-09-01', 50, 3000)
SELECT * FROM dual;
but that's just more typing and arguably maybe harder to read, and easier to get wrong.
You can read more about quoted and non-quoted identifiers in the documentation.
With a single row you don't really need ALL, you can also do:
INSERT INTO obat (id_obat,nama_obat,tanggal_kadarluarsa,stock,harga)
VALUES (1, 'Indomethacin', DATE '2023-09-01', 50, 3000)
Note that I've added the DATE keyword to these statements; '2023-09-01' is not a date, it's a string literal, so you're relying on Oracle implicitly converting that to an actual date, based on your current session NLS settings. With DATE '2023-09-01' that is now a date literal. Again, there is more in the documentation.
Related
I am trying to insert values to a nested table with an object of another table. This is what I'm trying (Sorry, I'm new working with dbs):
INSERT INTO Ocurrences (..., oSpace) VALUES
(other inserts,
/* insert I don't know to do it to nested table oSpaces */
);
How I could add a value in oSpaces inserting an object from table Spaces?
Thanks.
Just use a collection of REFerences:
INSERT INTO Ocurrences (
CCase,
/* ... Other column identifiers ..., */
oSpaces
) VALUES (
'abc',
/* ... Other column values ..., */
tSpace(
(SELECT REF(s) FROM spaces s WHERE s.intcode='1')
)
);
db<>fiddle here
As an aside, '20/02/2020' is not a DATE data-type, it is a string literal and relies on an implicit string-to-date conversion. This implicit conversion will fail if the user's NLS_DATE_FORMAT session parameter does not match your string's format and since any user can change their session parameters at any time then this is not something you should rely on.
Instead, you should use:
A date literal DATE '2020-02-20'; or
An explicit conversion TO_DATE('20/02/2020', 'DD-MM-YYYY').
In my main table having table structure column without double quotes but when in View column with double quotes. While inserting data from view into table then getting error.
Here is the table structure of tbl
create table tbl (ID number(10),
name varchar2(50),
addr varchar2(200));
While View is-
create or replace view t_view as
select "ID", "name", "addr" from tbl;
While inserting data into tbl from t_view -
insert into tbl
select * from t_view;
Then getting error ORA- 00904: "addr": Invalid identifier.
So how to resolve this issue, can i remove the double quotes from creation of view.
Remove all double quotes from everywhere in your code.
If you use them while creating objects, you'll have to use them always, specifying exactly same letter case.
Get rid of those, Oracle is - by default - case insensitive and treats all names as uppercase (but you can reference them any way you want, just don't use double quotes!).
Oracle stores the name of any object in UPPERCASE by default. If you have provided double quotes then only Oracle stores the name of the object as it is.
Double quotes - Case sensitive
No quotes - Case insensitive - Stores the name in UPPERCASE
In your case, While writing the DDL of the table, you have not provided the name of the column in double quotes so your table name and column names are stored in UPPERCASE in the metadata.
You can see the same using the following query
select table_name, column_name from user_tab_columns where table_name = 'TBL';
For giving your answer, create the view either of the following syntaxes:
create or replace view t_view as
select ID, name, addr from tbl; -- no double quotes
create or replace view t_view as
select "ID", "NAME", "ADDR" from tbl; -- UPPERCASE column names in double quotes
See the demo here
Cheers!!
What is it you are trying to accomplish. Your request just doesn't make sense. Your intended statement
insert into tbl select * from t_view;
accomplishes exactly the same thing as
insert into tbl select * from tbl;
It's not that you cann't do this (you can), but it can only cause massive duplicate data issues or (hopefully) unique constraint violations.
I have a table named masterregistry and it contains all the info and business logic in it and the data type of the colum is clob
desc master_registy:
id number not null,
name varchar2(100),
value clob
select value from master_registry where name='REG_DATE';
o/p
11-10-17
This date is common across all the business logic, I need to query my tables which has ,
desc get_employee
====================
id number not null,
first_name varchar2(100),
last_name varchar2(100),
last_mod_dt timestamp
Now I need to get all the values from the get_employee whose last_mod_dt should be greater than the value of master_registry where name='REG_DATE'.The value in the latter table is clob data, how to fetch and compare the date of a clob data against the timestamp from another table. Please help.
Maybe you need something like this.
SELECT *
FROM get_employee e
WHERE last_mod_dt > (SELECT TO_TIMESTAMP (TO_CHAR (VALUE), 'DD-MM-YY')
FROM master_registy m
WHERE m.id = e.id);
DEMO
Note that i have used the column value directly in TO_CHAR. You may have to use TRIM,SUBSTR or whatever required to get ONLY the date component.
I'm trying to create a table with date constraint that will make the date value of MM/YY for credit card expiration date. When I try to create the below table I receive the error:
CONSTRAINT exp_check CHECK TO_CHAR(exp_date,'MM/YY'),
*
ERROR at line 19:
ORA-00906: missing left parenthesis
Create table Orders
(o_id int NOT NULL,
c_id int NOT NULL,
p_id int NOT NULL,
s_id int NOT NULL,
order_date date DEFAULT sysdate,
o_qty number NOT NULL,
order_total number NOT NULL,
card_type varchar2(50) NOT NULL,
cc_number varchar2(50) NOT NULL,
exp_date date NOT NULL,
shipping_date date,
shipping_status varchar2(50) DEFAULT 'Not Shipped Yet',
UNIQUE (o_ID, c_ID, p_ID),
CONSTRAINT order_ID PRIMARY KEY (O_ID),
CONSTRAINT fk_cust_id FOREIGN KEY (C_ID) REFERENCES customers(C_ID),
CONSTRAINT fk_ship_id FOREIGN KEY (S_ID) REFERENCES shipping(S_ID),
CONSTRAINT qty_check CHECK (o_qty >= 0),
CONSTRAINT exp_check CHECK TO_CHAR(exp_date,'MM/YY'),
CONSTRAINT s_check CHECK (shipping_status IN
('Not shipped yet', 'Shipped', 'Delivered'))
);
Once I'm able to create the table I will create a executed stored procedure that will execute the below:
SET SERVEROUTPUT ON;
DECLARE
i_result varchar2(100);
BEGIN
place_order(2200,1,'Regular','VISA','1111-1111-2222-3333',11/19,1040,i_result);
END;
A date always has a day. month, year, hour, minute, and second. You could store a date that is midnight on the first of the month and validate that the exp_date = trunc(exp_date, 'MM'). When you display the value to the user, you'd then use the to_char expression to display it in the format that you want.
Alternately, you could store a string in the format MM/YY and your check constraint could put various validations on that string (i.e. the first two characters are a number between 1 and 12, the fourth and fifth characters are a number between 15 and 25, etc.).
As others have said, a DATE value in Oracle always has the fields YEAR, MONTH, DAY, HOURS, MINUTES, and SECONDS. I think what you want to do here is to store the last day/hour/minute/second of the expiration date in your EXP_DATE column, e.g. if expiration shown on the card is '07/19' you'd want to store 31-JUL-2019 23:59:59, which is the last possible second that the card is valid, in the EXP_DATE field. Then when you want to display the expiration date in its usual MM/YY format you just have to format it with TO_CHAR, e.g. TO_CHAR(EXP_DATE, 'MM/YY').
Best of luck.
In SQL we will be having a sequence. But it should be appended to a variable like this
M1,M2,M3,M4....
Any way of doing this ?
Consider having the prefix stored in a separate column in the table, e.g.:
CREATE TABLE mytable (
idprefix VARCHAR2(1) NOT NULL,
id NUMBER NOT NULL,
CONSTRAINT mypk PRIMARY KEY (idprefix, id)
);
In the application, or in a view, you can concatenate the values together. Or, in 11g you can create a virtual column that concatenates them.
I give it 99% odds that someone will say "we want to search for ID 12345 regardless of the prefix" and this design means you can have a nice index lookup instead of a "LIKE '%12345'".
select 'M' || my_sequence.nextval from dual;