How update Stored Procedure has Optional Parameter?
How can I alter Update Stored Procedure so if developer want to set only Size
then he/she does not requireed to pass TimeStamp
. Moreover, then what will be execute code for this procedure?
Scenario:
Update TimeStamp = getdate()
field whose SizeID = 1
AND Size =Large
Note: This field Parameters and condition parameters must be dynamic and optiona开发者_JS百科ls like in where TimeStamp
can also be supplied.
Update Stored Procedure:
ALTER PROCEDURE [dbo].[SP_Sizes_Update]
@SizeID int,
@Size varchar(50),
@TimeStamp datetime
AS
Update Sizes
Set
Size = @Size,
TimeStamp = @TimeStamp
Where
SizeID = @SizeID
one way
ALTER PROCEDURE [dbo].[SP_Sizes_Update]
@SizeID int,
@Size varchar(50) = 'Large',
@TimeStamp datetime = null
AS
Update Sizes
Set
Size = @Size,
TimeStamp = coalesce(@TimeStamp,getdate())
Where
SizeID = @SizeID
Just set the parameter to a default value, like this:
ALTER PROCEDURE [dbo].[SP_Sizes_Update]
@SizeID int,
@Size varchar(50),
@TimeStamp datetime = null
AS
Update Sizes
Set
Size = @Size,
TimeStamp = @TimeStamp
Where
SizeID = @SizeID
however, if you want the default value to be a result of a function call, you could use some special value that will be replaced by the function hole, like this:
ALTER PROCEDURE [dbo].[SP_Sizes_Update]
@SizeID int,
@Size varchar(50),
@TimeStamp datetime = null
AS
if @TimeStamp is null
set @TimeStamp = getdate()
Update Sizes
Set
Size = @Size,
TimeStamp = @TimeStamp
Where SizeID = @SizeID
精彩评论