Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check if row exists in table

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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.
Viewers will learn how the fundamental information of how to create a table.

704 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