我创建了存储过程,我想检查UserCategoryCode
和UserCategoryName
,现有记录,我是existing record
,但是没有为UserCategoryCode,
正确使用它我该怎么做?我试着这样做,但没有工作,(它的工作是UserCategoryName
)
SP
ALTER PROCEDURE [dbo].[UserCategories_InsertUpdate]
@UserCategoryId int,
@UserCategoryCode varchar(50),
@UserCategoryName varchar(250),
@Remarks nvarchar(max),
@StatusId int,
-- @StatusChangeDate DATETIME,
@CreateId int,
@Mode varchar(50),
@iOutput int output
AS
BEGIN
BEGIN TRY
BEGIN TRAN
IF @Mode = 'Add'
BEGIN
IF NOT EXISTS (SELECT UserCategoryCode,UserCategoryName FROM UserCategories WHERE UserCategoryCode = @UserCategoryCode AND UserCategoryName = @UserCategoryName)
BEGIN
INSERT INTO UserCategories(
UserCategoryCode,
UserCategoryName,
StatusId,
StatusChangeDate,
CreateBy,
CreatedDate,
Remarks
)
VALUES(
@UserCategoryCode,
@UserCategoryName,
@StatusId,GETDATE(),
@CreateId,GETDATE(),
@Remarks
)
SET @iOutput = 1 --save successful--
END
ELSE
BEGIN
SET @iOutput=-3 --existing record--
END
END
ELSE IF @Mode = 'Modify'
BEGIN
UPDATE UserCategories
SET UserCategoryCode = @UserCategoryCode,
UserCategoryName = @UserCategoryName,
StatusId = @StatusId,
Remarks = @Remarks,
EditBy = @CreateId,
EditDate = GETDATE()
WHERE UserCategoryId = @UserCategoryId
SET @iOutput = 2 --save successful--
END
COMMIT
END TRY
BEGIN CATCH
print ERROR_MESSAGE()
SET @iOutput = -2 --sp error--
ROLLBACK
END CATCH
END
答案 0 :(得分:1)
哦,最后它的工作,我找到了解决方案,
IF NOT EXISTS (SELECT UserCategoryCode FROM UserCategories WHERE UserCategoryCode = @UserCategoryCode)
IF NOT EXISTS (SELECT UserCategoryName FROM UserCategories WHERE UserCategoryName = @UserCategoryName)
BEGIN
INSERT INTO UserCategories(
UserCategoryCode,
UserCategoryName,
StatusId,
StatusChangeDate,
CreateBy,
CreatedDate,
Remarks
)
VALUES(
@UserCategoryCode,
@UserCategoryName,
@StatusId,GETDATE(),
@CreateId,GETDATE(),
@Remarks
)
SET @iOutput = 1 --save successful--
END
ELSE
BEGIN
SET @iOutput=-3 --existing record--
END
ELSE
BEGIN
SET @iOutput=-5 --existing record--
END
END
ELSE IF @Mode = 'Modify'
BEGIN
UPDATE UserCategories
SET UserCategoryCode = @UserCategoryCode,
UserCategoryName = @UserCategoryName,
StatusId = @StatusId,
Remarks = @Remarks,
EditBy = @CreateId,
EditDate = GETDATE()
WHERE UserCategoryId = @UserCategoryId
SET @iOutput = 2 --save successful--
END
COMMIT
END TRY
BEGIN CATCH
print ERROR_MESSAGE()
SET @iOutput = -2 --sp error--
ROLLBACK
END CATCH
END
和代码
else if (output == -3)
{
lblMsg.Text = "Already Exists!";
lblMsg.ForeColor = System.Drawing.Color.Orange;
}
else if (output == -5)
{
lblMsg.Text = "Already Exists!";
lblMsg.ForeColor = System.Drawing.Color.Orange;
}