我有这个SP:
ALTER PROCEDURE [dbo].[UpdateContractInfo](@UserGroupId int, @Quantity int, @ContractName varchar(150))
AS
BEGIN
UPDATE [contract]
SET totalUsers = @Quantity, contractName = @ContractName
WHERE userGroupId = @UserGroupId
END
我想稍微增强该查询以首先检查contractName列。 如果列为null,则继续更新contractName,否则跳过该字段的更新。 totalUsers字段应该以这种或那种方式更新。
由于
答案 0 :(得分:4)
试试这个
UPDATE [contract]
SET totalUsers = @Quantity,
contractName = CASE WHEN contractName IS NULL THEN @ContractName ELSE contractName END
WHERE userGroupId = @UserGroupId
答案 1 :(得分:3)
或者您可以尝试ISNULL()
功能
UPDATE [contract]
SET totalUsers = @Quantity,
contractName = ISNULL(contractName, @contractName)
WHERE userGroupId = @UserGroupId