I am writing a stored procedure that takes one input argument and can return the related attributes via the optional output parameters. It is defined like this:
CREATE PROCEDURE dbo.sp_get_info
@Identifier nvarchar(50),
@Info1 uniqueidentifier = NULL OUTPUT,
@Info2 nvarchar(10) = NULL OUTPUT,
@Info3 int = NULL OUTPUT
I would like to call the procedure like:
DECLARE @uid uniqueidentifier
...
EXEC sp_get_info @Idenfier = 'a123',
@Info1 = @uid
Getting each output value leads to a different activity. This way, I would like to detect the fact that only the @Info1 output value should be retrieved from elsewhere. This way, the procedure need not to execute possibly costly code that retrieves the other output (now unwanted) arguments.
Firstly, I was thinking about testing like IF @Info1 IS NOT NULL do_something. However, as the OUTPUT says also that the argument can have also the input value (Microsoft SQL) the @uid itself can have the value NULL. This way, the above test does not work.
Is there any technique used to solve the situation?
Thanks, Petr
You could use a
@fieldand@valueparameter. The@fieldcould beInfo1, Info2, Info3. Now the@valuecan benulland you still know what field to search.The calling code would have to convert the
@valuefromnvarchar(50)to the required type, but that’s typically easy.