Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12821
  • Last Modified:

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.
0
developer2012
Asked:
developer2012
  • 4
  • 4
  • 3
  • +1
2 Solutions
 
Haris DjulicCommented:
Where exists in (select * from page  WHERE form_id = '255626')
0
 
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')
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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.
0
 
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.
0
 
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.
0
 
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.
0
 
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.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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 (...)
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.
0
 
developer2012Author Commented:
Yes, Please close this queston.  Thanks.
0
 
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now