Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Stored Proc Problem
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01394196
Message ID:
01394256
Views:
45
The same way you did with a query and call it using named parameters.
EXEC UpdateSomething @col2='One'
....

CREATE PROCEDURE UpdateSomething
	@pk		INT,
	@Col1		int = NULL,
	@Col2		varchar = NULL,
	@Col3		char = NULL

AS
BEGIN

UPDATE mytable
	SET Col1 = ISNULL(@col1, col1)
	  Col2 = ISNULL(@col2, col2)
	  Col3 = ISNULL(@col3, col3)
WHERE pk = @pk

END

>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
>><pre>
>>...
>>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.
>>>
>>>
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform