• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3815
  • Last Modified:

Check if row exists in table

In Sproc how is best to determine if an Id exists in a datatable in SQL database and return true/false?
0
johnkainn
Asked:
johnkainn
  • 2
1 Solution
 
Aneesh RetnakaranDatabase AdministratorCommented:
IF EXISTS(SELECT 1 FROM urTable where<condition>  )
 return  1  ---row exists
0
 
pierkyCommented:
Try this...
IF EXISTS( SELECT ID FROM tbl WHERE ID = 1 )
BEGIN
...
END

Open in new window

0
 
Patrick MatthewsCommented:
Hello johnkainn,

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


Regards,

Patrick
0
 
Patrick MatthewsCommented:
Wow, three responses within 43 seconds :)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now