Why do we use "/" to mark the end of a MySQL query? - oracle

create type emp_t AS OBJECT(
EMPNO CHAR(6),
FIRSTNAME VARCHAR(12),
LASTNAME VARCHAR(15),
WORKDEPT REF dept_t,
SEX CHAR(1),
BIRTHDAY DATE,
SALARY NUMBER(8,2)
)
/ <--------- this one

As the comment said, that is used as a Delimiter, but it has to be assigned first.
The normal delimiter is (;), you can set a new one by adding the command and then the sign you want to use.
Once change the delimiter, you can use the new delimiter to end a statement as follows:
DELIMITER //
SELECT * FROM customers //
SELECT * FROM products //
Here is some Documentation.

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 use NVL in PL/SQL for Date columns?

This is my first SP in Oracle.
create or replace PROCEDURE SEARCH(R1 OUT SYS_REFCURSOR,
UserID IN VARCHAR2,
Name IN VARCHAR2,
FromDate IN VARCHAR2,
ToDate IN VARCHAR2
)
IS
BEGIN
OPEN R1 FOR
SELECT * FROM USER WHERE id = NVL( UserID, id )
and ((LASTNAME =NVL(Name,LASTNAME)) OR( FIRSTNAME =NVL(Name, FIRSTNAME)))
and ( to_char(releaseddate, 'mm/dd/rrrr') between FromDate and ToDate)
order by RELEASEDDATE desc
FETCH FIRST 100 ROWS ONLY;
END SEARCH;
In my table, I have columns Id, FirstName, LastName, and ReleasedDate. These columns are not dependent on each other. I used NVL to handle the Table columns, but RleasedDate column depends on FromDate and To date input parameters. I tried to use NVL, but I am facing issues. I am receiving the data in mm/dd/rrrr format. Is there any way that I use NVL for Releaseddate ?
Currently, I am receiving all the data from table where releaseddate is null and I am trying to avoid it.
Thanks for the help in advance.
and ( to_char(releaseddate, 'mm/dd/rrrr') between FromDate and ToDate)
I wouldn't use nvl for this, partly because it would exclude column values that are null. I would test explicitly for null arguments; something like:
OPEN R1 FOR
SELECT * FROM USER
WHERE (UserID IS NULL OR id = UserID)
AND (Name IS NULL OR LASTNAME = Name OR FIRSTNAME = Name)
AND (FromDate IS NULL OR releaseddate >= TO_DATE(FromDate, 'dd/mm/rrrr'))
AND (ToDate IS NULL OR releaseddate <= TO_DATE(ToDate, 'dd/mm/rrrr'))
ORDER BY RELEASEDDATE DESC
FETCH FIRST 100 ROWS ONLY;
This compares the releasedate column values as a date, rather than converting it to a string; instead the string arguments are converted to dates to match the column data type, which is more efficient. It would be better to have the procedure arguments declared as dates, and make the caller provide a valid value.
Don't try to convert dates to character strings in order to compare them - you'll just get unexpected results. Instead, convert the character strings to dates.
As far as RELEASEDATE being NULL - I'd just use a condition to test for this. Assuming that you want to include the rows where RELEASEDATE is NULL I suggest:
create or replace PROCEDURE SEARCH(R1 OUT SYS_REFCURSOR,
UserID IN VARCHAR2,
Name IN VARCHAR2,
FromDate IN VARCHAR2,
ToDate IN VARCHAR2)
IS
BEGIN
OPEN R1 FOR
SELECT *
FROM USER
WHERE id = NVL( UserID, id ) and
( LASTNAME = NVL(Name, LASTNAME) OR
FIRSTNAME = NVL(Name, FIRSTNAME) ) and
( REALEASEDDATE IS NULL OR
REALEASEDDATE between TO_DATE(FromDate, 'mm/dd/rrrr')
and TO_DATE(ToDate, 'mm/dd/rrrr') )
order by REALEASEDDATE desc
FETCH FIRST 100 ROWS ONLY;
END SEARCH;

How to use ANY command in SQL

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);

Decode() in Oracle

What is the syntax for decode() function of oracle to encrypt string in
Example :- if i want to encrypt 'suvendu' with'***' and 'mohan' with '$$$' for column fname of samples table
desc samples
Name Null Type
------ -------- ------------
EMP_ID VARCHAR2(20)
LNAME CHAR(10)
FNAME CHAR(20)
DEPT CHAR(20)
SAL NOT NULL NUMBER(12,2)
H_DATE DATE
EMAIL VARCHAR2(20)
DESG VARCHAR2(25)
While you can use DECODE to handle this, as in:
SELECT DECODE(FNAME,
'suvendu', '***',
'mohan', '$$$',
FNAME) AS DERIVED_COL
FROM SAMPLES
IMO using a CASE expression is a better choice
SELECT CASE FNAME
WHEN 'suvendu' THEN '***'
WHEN 'mohan' THEN '$$$'
ELSE FNAME
END AS DERIVED_COL
FROM SAMPLES
as it makes it clearer what's going on and is easier to read.
Best of luck.
Updated(try this):
SELECT
DECODE(fname,'suvendu', '***', 'mohan", '$$$') AS F_NAME
--add other columns/column-list here if needed
FROM
samples
The line starting with -- is a commented line

Substitution variable seems to fail in a query to describe an oracle table

I am trying to describe a table without using the DESCRIBE command but I want to combine the query with a substitution variable. Assuming I have the following table:
--DROP TABLE customers CASCADE CONSTRAINTS PURGE;
CREATE TABLE customers
( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50)
);
Following the posts here and here but adding a substitution variable, I have the following:
ACCEPT myv CHAR PROMPT 'Enter a table name: '
SELECT
column_name AS "Name",
nullable AS "Null?",
concat(concat(concat(data_type,'('),data_length),')') AS "Type"
FROM user_tab_columns
WHERE table_name = '&myv';
This returns a blank table with the appropriate column names. It doesn't matter if I entered the table name in the input prompt as CUSTOMERS or customers. However, desc customers yields:
Name Null Type
------------- -------- ------------
CUSTOMER_ID NOT NULL NUMBER(10)
CUSTOMER_NAME NOT NULL VARCHAR2(50)
CITY VARCHAR2(50)
Any idea how I can get substitution variable to work here? Thanks.
I got it to work with a bind variable. Not exactly sure what is going on because #kordirko said the query worked for him as is. Anyway, to get it to work for me (I'm using SQL Developer Version 4.0.3.16), I used bind variable, like so:
SELECT
column_name "Name",
nullable "Null?",
concat(concat(concat(data_type,'('),data_length),')') AS "Type"
FROM user_tab_columns
WHERE table_name = :myv;
I then entered CUSTOMERS into the value field of the Enter Binds window and the query executed fine. If anybody knows why substitution variable failed but bind variable did not, that will certainly add to the discussion.

Resources