How to use ANY command in SQL - oracle

I have a following query description. How to express it in SQL?
Query -> list all the employee first name such that their emp_pct is less than any of the employee whose proj_num is 18 (Make use of ANY).
I have following table name EMP_2;
Name Type
-----------------------------------
EMP_NUM CHAR(3)
EMP_LNAME CHAR(15)
EMP_FNAME CHAR(15)
EMP_INITIAL CHAR(1)
EMP_HIREDATE DATE
JOB_CODE CHAR(3)
EMP_PCT NUMBER(5,2)
PROJ_NUM CHAR(3)

Here you go. The ANY keyword compares and short circuits the moment it finds a matching row in the sub-query.
SELECT a.EMP_FNAME
FROM EMP_2 a
WHERE a.EMP_PCT < ANY (
select EMP_PCT
from EMP_2
where EMP_NUM <> a.EMP_NUM and PROJ_NUM = 18);

Related

oracle sql developer after creating table , column name came as column1,column2 .... columnN

i am using sql developer Version 21.2.1.204 and when i create table by using new table function in IDE , and entered name and select type for each field
after clicking on ok button table is created but fields name are different,
COLUMN2,
COLUMN3,
COLUMN4,
came
i entered column name properly but still this is happening
you can see in images
If the wizard is not working for you, you can write out the DDL statement in a worksheet:
CREATE TABLE table1 (
id VARCHAR2(20),
name VARCHAR2(20),
address VARCHAR2(20),
age VARCHAR2(20) -- Why not use a number?
);
And run it.
As an aside, you probably should use date_of_birth and then calculate the age rather than using an age column that will go out of date as soon as the next person reaches their birthday.
CREATE TABLE table1 (
id VARCHAR2(20),
name VARCHAR2(20),
address VARCHAR2(20),
date_of_birth DATE
);

How to get the value of clob data which has date field and compare with timestamp in oracle

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.

invalid number error when inserting a row in oracle

I need to insert a row to a table in oracle.
insert into policy_tab values ('4325','29-APR-98','29-APR-2007',32424,(select ref(a) from agent_tab a where a.nic='242424v'),claim_ntty(
claim_t('25-APR-2005','25-JUN-2005'),
claim_t('26-APR-2005','26-JUN-2005')
));
But when I executed it will show this error. "ORA-01722: invalid number ORA-06512: at "SYS.DBMS_SQL", line 1721"
create type policy_ty as object(
pid char(5),
sDate date,
eDate date,
premium number(10,2),
agent ref agent_ty,
claims claim_ntty
);
create table policy_tab of policy_ty(
pid primary key,
agent SCOPE IS agent_tab
)
nested table claims store as claim_nttab;
create type claim_t AS OBJECT(
eDate date,
amount number(10,2)
);
create type claim_ntty as table of claim_t;
create type agent_ty as object(
nic char(10),
name varchar(50),
address varchar(50),
contactNo contactNo_vaty
) NOT FINAL;
create table agent_tab of agent_ty(
nic primary key
);
So how to resolve it?
From the first look you need to use like below. However the information provided is not sufficient to resolve the issue. Related Objects defintion as well needed.
This has to be changed - '29-APR-98' to '29-APR-1998'
INSERT INTO policy_tab
VALUES (
'4325',
'29-APR-1998',
'29-APR-2007',
32424,
(SELECT REF (a)
FROM agent_tab a
WHERE a.nic = '242424v'),
claim_ntty (claim_t ('25-APR-2005', '25-JUN-2005'),
claim_t ('26-APR-2005', '26-JUN-2005')));
Edit:
Second observation. You created the below object:
create type claim_t AS OBJECT(
eDate date,
amount number(10,2)
);
And using it like:
claim_ntty (claim_t ('25-APR-2005', '25-JUN-2005'),
claim_t ('26-APR-2005', '26-JUN-2005')));
Second Argument should be number not date.
So your insert should be:
INSERT INTO policy_tab
VALUES (
'4325',
'29-APR-1998',
'29-APR-2007',
32424,
(SELECT REF (a)
FROM agent_tab a
WHERE a.nic = '242424v'),
claim_ntty (claim_t ('25-APR-2005', 123), claim_t ('26-APR-2005', 456)));

how to get ref value of object table in oracle

create table mbastudent
(
semester varchar2(20) ,
stud_info student
);
this is object table i have created
now iserting value--
insert into mbastudent
(semester,stud_info )
values
('1st' ,student(1100 ,'KUMAR', '07-OCT-80', '04-MAR-14', 15000))
insert into mbastudent
(semester,stud_info )
values ('2nd',student(1101,'SESHU', '07-OCT-81', '04-MAR-14', 15000));
select ref(a) from mbastudent a;
--how to get ref value of object
You may find it in
Oracle docs from site
I dont now structure of student. My sugesstion is
student (
id number,
name varchar2(4000),
birthday date,
begin_date date,
cost number
)
And you want to get a name, cost and semester
SELECT semester, stud_info.name, stud_info.cost
FROM mbastudent s

Where does oracle store my object instances?

I've created the following two object types :
create or replace type person_typ as object (
person# varchar(10)
) not final;
create or replace type salesperson_typ under person_typ (
salesperson# varchar(10),
sSurname varchar(10),
sForename varchar(10),
dateOfBirth date
);
create table person_tab of person_typ (
person# primary key
);
And I've inserted a row using :
insert into person_tab
values (salesperson_typ('p1','s1', 'Jones', 'John', sysdate));
Which I can retrieve using the following :
select
treat(value(s) as salesperson_typ).person# as person_number,
treat(value(s) as salesperson_typ).sSurname as sSurname
from
person_tab s
;
However, if I look at person_tab I only see the following :
SQL> select * from person_tab;
PERSON#
----------
p1
I'm curious, where does the salesperson specific data get stored? I was almost expecting to find a salesperson table, but I can't find anything obvious.
Your object is stored invisibly in the same table.
You can check columns by querying USER_TAB_COLS:
SELECT *
FROM user_tab_cols
WHERE table_name = 'PERSON_TAB';
Then you can then use the column names* you just discovered in a query (except SYS_NC_ROWINFO$, that throws an error for me).
SELECT SYS_NC_OID$
,SYS_NC_TYPEID$
--,SYS_NC_ROWINFO$
,PERSON#
,SYS_NC00005$
,SYS_NC00006$
,SYS_NC00007$
,SYS_NC00008$
FROM PERSON_TAB;
Note*
You should not use these column names in any application because they are internal and subject to change in future patches/releases.

Resources