What happens when I update the SQL column myself? -Or-simplified conditional updates?

I would like to call the "update" stored procedure, which will not necessarily include all columns. There is probably a better way to handle this .... As you can see, if I don't pass the column parameters, their value will be NULL. Then, using ISNULL, I set the columns to both my new values ​​and 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
+3
source share
1 answer

, . Microsoft , , ?: -)

, , . , . , .
+2

Source: https://habr.com/ru/post/1767056/


All Articles