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.
Usage. IN is used as multiple OR operator whereas EXISTS helps to find whether any value is returned or not.
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.
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.
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.
The equivalent would be:
select count(*)
from dual
where exists (SELECT * FROM theTable where theColumn like 'theValue%')
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With