Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Select Exists in Oracle?

Tags:

sql

oracle

What is the equivalent of the below SQL Query in Oracle?

SELECT CAST(
   CASE WHEN EXISTS(SELECT * FROM theTable where theColumn like 'theValue%') THEN 1 
   ELSE 0 
   END 
AS BIT)

I just want an oracle query where exists is used and it returns 0 or 1 like above.

like image 464
The Light Avatar asked May 15 '13 10:05

The Light


People also ask

How do you use EXISTS instead of in in Oracle?

Usage. IN is used as multiple OR operator whereas EXISTS helps to find whether any value is returned or not.

What is the use of EXISTS in Oracle?

The EXISTS function in Oracle checks to find a single matching row to return the result in a subquery. Because the IN function retrieves and checks all rows, it is slower.

How do you check if a row EXISTS in Oracle?

Type a short Oracle program, using the following code as a guide: DECLARE record_exists INTEGER; BEGIN SELECT COUNT(*) INTO record_exists FROM your_table WHERE search_field = 'search value' AND ROWNUM = 1; IF record_exists = 1 THEN DBMS_OUTPUT.

How do you check a table EXISTS in Oracle?

You can also check the data dictionary to see if a table exists: SQL> select table_name from user_tables where table_name='MYTABLE'; Another way to test if a table exists is to try to drop the table and catch the exception if it does not exist. and include the URL for the page.


2 Answers

The equivalent would be:

select count(*) 
from dual 
where exists (SELECT * FROM theTable where theColumn like 'theValue%')
like image 126
Florin stands with Ukraine Avatar answered Oct 12 '22 23:10

Florin stands with Ukraine


This would show the same output. Just removed the CAST and added a FROM dual as Oracle doesn't allow queries with SELECT and without FROM:

SELECT 
   CASE WHEN EXISTS(SELECT * FROM theTable where theColumn like 'theValue%') 
     THEN 1 
     ELSE 0 
   END 
FROM dual ;

Tested at SQL-Fiddle

like image 25
ypercubeᵀᴹ Avatar answered Oct 13 '22 00:10

ypercubeᵀᴹ