What happens when I update SQL column to itself? -OR- simplified conditional updates?
I would like to call an "update" stored procedure which won't necessarily include all columns. There is probably a better way to handle this.... As you can see, if I do开发者_如何转开发 not pass in the column parameters their value is NULL. Then, using the ISNULL, I set the columns either to their new values or their existing values.
CREATE PROCEDURE [dbo].[spUpdateTable]
@pPKID bigint = NULL,
@pColumn1 int = NULL,
@pColumn2 int = NULL
AS
BEGIN
SET NOCOUNT ON;
UPDATE
TableName
SET
[Column1] = ISNULL(@pColumn1,[Column1]),
[Column2] = ISNULL(@pColumn2,[Column2])
WHERE
[PKID] = @pPKID
END
This is basically the same thing that the transactional replication stored procedures do when updating a table on a subscriber. If Microsoft does it themselves, it must be safe, right? :-)
Seriously, my primary concern here would be any update triggers that might exist on the table. You'd want to understand the impact of potentially firing those triggers on what could be a non-change. Otherwise I think your technique is fine.
精彩评论