Solved

Check if row exists in table

Posted on 2008-06-25
4
3,780 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

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!

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

729 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