在sql server中增加int 1?

问题描述:

如果以下SQL返回NULL,如何返回1在sql server中增加int 1?

类似的信息(伪代码):

if sql return NULL value 
then set value to one 
otherwise returning sql result value. 

是否有定义为1的默认值,如果SQL结果为NULL任何SQL?

SELECT Max(iCategoryOrder)+1 
FROM [IVRFlowManager].[dbo].[tblCategory] 
WHERE iCategoryLevel = 1 

使用ISNULL运算符,如:

ISNULL(your_field, 1) 

尝试以下操作:

Select ISNULL(Max(iCategoryOrder), 0) + 1 
from [IVRFlowManager].[dbo].[tblCategory] 
where iCategoryLevel = 1 
+0

感谢您的快速响应,这正是我想要的,再次感谢 – 2012-04-10 11:42:03

选项1

使用ISNULL()

说明

用指定的替换值替换NULL。

SELECT MAX(ISNULL(iCategoryOrder, 0))+1 
FROM  [IVRFlowManager].[dbo].[tblCategory] 
WHERE  iCategoryLevel = 1 

选项2

使用COALESCE()

SELECT MAX(COALESCE(iCategoryOrder, 0))+1 
FROM  [IVRFlowManager].[dbo].[tblCategory] 
WHERE  iCategoryLevel = 1 

说明

返回第一个非空expressio在其论据中。

+0

提及COALESCE(),这在我看来是更好的选择,因为它是标准的SQL – fancyPants 2012-04-10 12:55:02