Solved

Check if row exists in table

Posted on 2008-06-25
4
3,734 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
  • 2
4 Comments
 
LVL 75

Accepted Solution

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

Expert Comment

by:pierky
Comment Utility
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
Comment Utility
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
Comment Utility
Wow, three responses within 43 seconds :)
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now