Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check if row exists in table

Posted on 2008-06-25
4
Medium Priority
?
3,802 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 500 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 93

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 93

Expert Comment

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

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

916 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