Solved

Check if row exists in table

Posted on 2008-06-25
4
3,771 Views
Last Modified: 2012-08-13
In Sproc how is best to determine if an Id exists in a datatable in SQL database and return true/false?
0
Comment
Question by:johnkainn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 125 total points
ID: 21864511
IF EXISTS(SELECT 1 FROM urTable where<condition>  )
 return  1  ---row exists
0
 
LVL 2

Expert Comment

by:pierky
ID: 21864515
Try this...
IF EXISTS( SELECT ID FROM tbl WHERE ID = 1 )
BEGIN
...
END

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 21864519
Hello johnkainn,

IF EXISTS (SELECT ID FROM YourTable WHERE ID = @ID_to_look_for)
      @ID_exists = 1
ELSE
      @ID_exists = 0


Regards,

Patrick
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 21864531
Wow, three responses within 43 seconds :)
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question