Check if record exists

If you get an Id as a parameter you may want to check if the related record really exist and/or if it is active.

declare @emailAddress varchchar(200) = ?
-- [if null then fail]

if( NOT EXISTS ( 
		  // The selected record must exist
			SELECT UserID	FROM UserTabel WHERE EmailAddress = @emailAddress AND IsActive =1)
		) RAISERROR ('Unknown E-Mail Address "%s".', 17, 0, @emailAddress)

-- Result set
select   * from Invoices 
	where  EMailAddress = @emailAddress

Last updated