Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Select and return PK value
Message
 
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
01673328
Message ID:
01673334
Views:
30
>>>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
>>>              -- return error
>>>       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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform