我有一个存储的产品,如下所示。如果我修改了
IF (@id!=0)
到
IF (@id!=null)
当我传递200这样的id时,IF (@id!=null)
将返回false,将执行插入部分。为什么?我应该修改@id as int=null
到@id as int=0
吗?
ALTER PROCEDURE [dbo].[spname]
-- Add the parameters for the stored procedure here
@id as int=null,
@vendorName as varchar(150)=null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF (@id != 0) -- update
BEGIN
...
END
ELSE -- insert
BEGIN
...
END
END
答案 0 :(得分:3)
在SQL中,您无法使用Null
或=
运算符测试!=
。您必须改为使用Is Null
或Is Not Null
。
ALTER PROCEDURE [dbo].[spname]
-- Add the parameters for the stored procedure here
@id as int = null,
@vendorName as varchar(150) = null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF @id Is Not Null -- update
BEGIN
...
END
ELSE -- insert
BEGIN
...
END
END
答案 1 :(得分:0)
在编写任何SP之前使用SET ANSI_NULLS OFF
,
或者
转到数据库属性>>选项,设置ANSI NULLS Enabled
使其适用于所需的数据库。
IF(@ID != 0)
可行。