cheryl9063
asked on
SQL Set variable if 1 record exists
See query example below.. I want to set a variable if only 1 record exists.. If no records exists I want to do something if more than 1 record exists I want to do something else..
Set @Lid = (Select Lid from table)--- only if 1 record exists
If @@Rowcount < 1
Print 'error'
If @@Rowcount > 1
Print 'different error'
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if exists(select Lid from table)
if exists(select top 2 Lid from table)
Print 'different error - > 1 records exist'
else
select @Lid = Lid from table
else
Print 'no records exist'
if exists(select top 2 Lid from table)
Print 'different error - > 1 records exist'
else
select @Lid = Lid from table
else
Print 'no records exist'
ASKER
Thank you.. The only problem with this solution is I would have to do the query twice correct? My real query is pretty big and involves several joins..
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
Glad to help
Open in new window