提问人:morganherg 提问时间:2/17/2022 最后编辑:marc_smorganherg 更新时间:2/17/2022 访问量:245
如何使存储过程参数向后兼容?
How to make a stored procedure parameter backwards compatible?
问:
我有一个接受以下参数的存储过程:
@ExecutionID INT,
@Status_Name VARCHAR(50),
我们需要更改过程,使其接受多个 ID,而不仅仅是 ,所以现在参数如下所示:ExecutionID
@Id INT,
@Status_Name VARCHAR(50),
@IdType BIT = 0 --0 for ExecutionID and 1 for InsertID)
SELECT TOP 1 @Status_Curent = [' + @Status_Name + '_Status]
FROM [Database].[dbo].[StoredProcedure]
WHERE [' + CASE WHEN COALESCE(@IdType, 0) = 0 THEN 'Execution' ELSE 'Insert' END + 'ID] = ' + CAST(@Id AS VARCHAR(50)) + '
ORDER BY [' + @Status_Name + '_Status] DESC
但是我们仍然有其他项目运行旧程序。我怎样才能默认运行,就好像它是一样,这样我们就不必更改其他项目了?@ExecutionID
@Id
到目前为止,我有这个:
DECLARE @Status_Name VARCHAR(50) = 'Food',
@IdType BIT = 0,
@Id INT = NULL,
@ExecutionID INT = 6045
IF (@ExecutionID != NULL)
BEGIN
SET @Id = @ExecutionID
END
SELECT @Id
或
SET @Id = CASE WHEN @ExecutionID != NULL THEN @ExecutionID ELSE @Id END
但它们仍然返回 NULL。
提前致谢
答:
1赞
marc_s
2/17/2022
#1
您不能将变量与使用通常的相等或不等式运算符进行比较 - 这些运算符将始终返回 / undefed。NULL
NULL
您需要专门使用 or - 因此请尝试以下操作:@ExecutionID IS NULL
@ExecutionID IS NOT NULL
DECLARE @Status_Name VARCHAR(50) = 'Food',
@IdType BIT = 0,
@Id INT = NULL,
@ExecutionID INT = 6045
IF (@ExecutionID IS NOT NULL)
BEGIN
SET @Id = @ExecutionID
END
SELECT @Id
和
SET @Id = CASE
WHEN @ExecutionID IS NOT NULL
THEN @ExecutionID
ELSE @Id
END
评论
NULL
@ExecutionID IS NULL
@ExecutionID IS NOT NULL