我试图将记录添加到表中,并且当记录已经存在时,我希望它返回-3。但是在我的C#代码中,SQL Server返回DBNull
。不知道为什么,因为它返回Scope_Identity
没问题。这是我的SQL,感谢任何帮助。
ALTER PROCEDURE [dbo].[InsertType]
@Name varchar(20),
@UserName varchar(12),
@returnID int output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF EXISTS (SELECT Name FROM tblTypes
WHERE UserName = @UserName AND Name = @Name)
BEGIN
RETURN -3 --Already exists
END
ELSE
BEGIN
INSERT INTO tblTypes (Name, UserName)
VALUES (@Name, @UserName)
SET @returnID = SCOPE_IDENTITY()
RETURN @returnID
END
END