Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Stored Proc Problem
Message
From
10/04/2009 17:23:29
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01394196
Message ID:
01394259
Views:
38
Kevin,
You have two options. The first is to use named parameters so your Stored Proc KNOWS that this parameter you are passing in maps to the definition in your Stored Proc.

Your second option is to pass your parameters by index, but if you have a null value you have to explicitly pass NULL. Here's an example:

Stored Proc Header:
CREATE PROCEDURE usp_MyStored Proc
 (  
   @Parameter1 int = NULL,
   @Parameter 2 int = NULL
   @Parameter 3 int = NULL
)
AS
.....
Calls:
EXEC usp_MyStoredProc 1, 2, 3; --You are passing in 3 values by index.  There are three parameters so life is good.
EXEC usp_MyStoredProc NULL, 2, 3; --You only want to pass in Parameter2 and Parameter3, but you need a filler for the first parameter since this is passing in parameters by index
EXEC usp_MyStoredProc @Parameter2 = 2, @Parameter3 = 3; --You only want to pass in Parameter2 and Parameter3, and since we are using named parameters the Stored Procedure knows what goes where.
>How do I create an update procedure that works the same way? I want to pass only the parameters for the changed columns.
>
>Thanks
>
>
>
>>You cannot skip positional parameters. It should be
>>
>>...
>>EXEC bm_GetClients NULL, @ClientKey
>>--or
>>...
>>SET  @CompanyKey = NULL
>>EXEC bm_GetClients @CompanyKey, @ClientKey
>>
>>
>>
>>Named parameters will work as well
>>
>>
>>EXEC bm_GetClients @ClientKey =  @ClientKey
>>
>>
>>>Ok, here it is.
>>>
>>>
>>>SET ANSI_NULLS ON
>>>GO
>>>SET QUOTED_IDENTIFIER ON
>>>GO
>>>
>>>CREATE PROCEDURE bm_GetClients
>>>	@CompanyKey		INT = NULL,
>>>	@ClientKey		INT = NULL
>>>
>>>AS
>>>BEGIN
>>>
>>>	SELECT *
>>>		FROM bm_Clients
>>>		WHERE (CompanyKey = @CompanyKey OR @CompanyKey IS NULL) AND
>>>			  (ClientKey = @ClientKey OR @ClientKey IS NULL)
>>>END
>>>
>>>
>>>If I call it with:
>>>
>>>DECLARE @CompanyKey INT
>>>DECLARE @ClientKey INT
>>>SET  @CompanyKey = 2
>>>SET @ClientKey = 5
>>>EXEC  bm_GetClients @CompanyKey = @CompanyKey, @ClientKey = @ClientKey
>>>
>>>
>>>Then it works ok.
>>>
>>>
Very fitting: http://xkcd.com/386/
Previous
Reply
Map
View

Click here to load this message in the networking platform