Check using RowCount

If you don’t want to check for existence only, but if you want to ensure there is exactly one match, counting is bettern the checking for existence.

declare @rowCount int
select          @rowCount = count(1)
        from    Entity
        where   Entity = @entity
                AND RecordState=0

if @rowCount > 1 RAISERROR( N'Ambiguous Entity %s!', 17, 0, @entity)

Last updated