我有一个用于插入的存储过程。当我 我正在插入一条记录,我希望发布的价值 等于超过最高值的一个。所以在这里 是我编码的:
DECLARE @MaxRelease int;
SELECT @MaxRelease = max(Release)
FROM dbo.Test
WHERE TestTypeId = @TestTypeId
AND ExamId = @ExamId
AND Title = @Title;
INSERT ... @MaxRelease + 1 ... etc.
但是我怎么能这样做,以便如果SELECT没有返回任何内容(因为 没有当前的记录)@MaxRelease的值是0?
答案 0 :(得分:2)
您可以使用ISNULL (Transact-SQL)将空值替换为替换值。
SELECT @MaxRelease = isnull(max(Release), 0)
FROM dbo.Test
答案 1 :(得分:1)
将@MaxRelease指定为初始值零。
DECLARE @MaxRelease int = 0;
SELECT @MaxRelease = max(Release)
FROM dbo.Test
WHERE TestTypeId = @TestTypeId
AND ExamId = @ExamId
AND Title = @Title
GROUP BY TestTypeId;
如果select中没有返回任何内容,则不会修改@MaxRelease。
答案 2 :(得分:1)
试试这个
DECLARE @MaxRelease int;
SELECT @MaxRelease = max(Release)
FROM dbo.Test
WHERE TestTypeId = @TestTypeId
AND ExamId = @ExamId
AND Title = @Title;
INSERT ... CASE WHEN @MaxRelease IS NULL THEN 0 ELSE @MaxRelease + 1 END ... etc.
答案 3 :(得分:0)