>>>Hi,
>>>
>>>My stored procedure receives a value of an equipment ID (optional; that is, an empty string could be passed).
>>>In the SP I want to find if such ID exists and if so, set the value of the equipment PK.
>>>How do I test if the value of @EquipPk is NULL (record not found) on one line with the SELECT?
>>>
>>>
>>>IF @EquipmentID <> '' and @EquipPk = (SELECT EQUIP_PK FROM EQ_FILE where ID_NUMBER = @EquipmentID)
>>> BEGIN
>>>
>>> END
>>>
>>>
>>>TIA
>>
>>Why does it have to be one line?
>>
>>if COALESCE(@EquipmentId,'') !=''
>> begin
>> select @Pk = equip_pk from dbo.eq_file where Id_Number = @EquipmentId;
>> if @Pk==null
>> begin
>> raieserror ...
>> return
>> end
>> end
>
>I thought that it could be all done in just one line. But your code, I am sure, will work.
>Thank you.
>A follow up question. I usually compare to null using if @pk is null and you used ==. Is there much difference in these?
It is actually my mistake. You're correct, you should use if @pk IS NULL.
(I got C# syntax instead)
If it's not broken, fix it until it is.
My Blog