How to use if exists in ORACLE/SQL?

I am trying to execute a query with if exist and it returns an error. Error Report

and the query is
IF Exists(select * from page  WHERE form_id = '255626');

Open in new window


How can I resolve this?  I am just trying to check whether the records exists or not?

Thanks.
developer2012Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Haris DulicIT ArchitectCommented:
Where exists in (select * from page  WHERE form_id = '255626')
developer2012Author Commented:
Can you please elobrate it?  I mean where should I put where exists in?

select id from pages
where exists in(select * from pages WHERE form_id = '255626')
Jim HornSQL Server Data DudeCommented:
If you're doing this as a decision structure, then it's IF EXISTS(...).  To demonstrate..
CREATE TABLE page (form_id int) 

INSERT INTO page (form_id) VALUES (1) 

IF EXISTS (SELECT * FROM page WHERE form_id=255626) 
   PRINT 'TRUE'
ELSE
   PRINT 'FALSE'
	
INSERT INTO page (form_id) VALUES (255626) 

IF EXISTS (SELECT * FROM page WHERE form_id=255626) 
   PRINT 'TRUE'
ELSE
   PRINT 'FALSE'

Open in new window


If you're doing it as a query with only one table, then it's WHERE form_id=255626

If you're doing it as a query with multiple tables, spell out all the tables we're talking about.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

slightwv (䄆 Netminder) Commented:
What product/software are you using?

You mention Oracle/SQL in the title but there is no 'if exists' in Oracle.

From the sample you posted above, Oracle syntax is:
 select id from pages
 where exists (select 1 from pages WHERE form_id = '255626') 

Open in new window


I hope that is just an example because that query really doesn't make sense.
developer2012Author Commented:
I want to check whether the record exists or not of certain value.

For example there are some values which does not exist in the table then how can I check it?

Thanks.
slightwv (䄆 Netminder) Commented:
Connect to the database using sqlplus:
select id from pages WHERE form_id = '255626'

You either get rows or you don't.  If you don't then 255626 doesn't exist.

Other than that, I don't understand the question.

If you want to check in code, what programming language?

We need more information to provide a more exact answer.
developer2012Author Commented:
Well, I am using c# and I would like to check whether the ID '255626' exist or not in the column form ID.
slightwv (䄆 Netminder) Commented:
I wish you hadn't accepted this.

First the SQL in the first post, http:#a40368986 isn't even valid Oracle syntax.
The second accepted answer won't work for C#.

Here is how it should be done in C# (ODP.Net example):
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "select count(*) from page  WHERE form_id = :myid and rownum=1";
cmd.CommandType = CommandType.Text;

OracleParameter parm = new OracleParameter();
parm.OracleDbType = OracleDbType.Decimal;
parm.Value = "2556256";

Then to get the value:
cmd.ExecuteScalar()

This will return either a 0 or 1.
Jim HornSQL Server Data DudeCommented:
In my above comment I neglected to add a disclaimer 'SQL Server, no clue about Oracle...'.

>First the SQL in the first post, http:#a40368986 isn't even valid Oracle syntax.
Not SQL either, as WHERE EXISTS IN isn't valid, it's either WHERE EXISTS(...) or WHERE column_name IN (...)
Jim HornSQL Server Data DudeCommented:
developer2012 - Can you tell us how the first answer solved your question?
Just curious, as it appears that it doesn't work in either Oracle or SQL.
developer2012Author Commented:
Yes, Please close this queston.  Thanks.
slightwv (䄆 Netminder) Commented:
>>Yes, Please close this queston.  Thanks.

It is up to you to properly close your questions by accepting the post or posts that provided the answer.

The reason this was re-opened is that one of the posts you selected as an answer wasn't even valid syntax so there was no way it could have helped answer the question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.