Many times you're required to write query to determine if a record exists. Typically you use this to determine whether to insert or update a records. Using the
EXISTS keyword is a great way to accomplish this.
if EXISTS (select *
where au_id = '172-32-1176')
Print 'Record exits - Update'
Print 'Record doesn''t exist - Insert'
EXISTS function takes one parameter which is a SQL statement. If any records exist that match the criteria it returns true, otherwise it returns false. This gives you a clean, efficient way to write a stored procedure that does either an insert or update.