我有一个存储过程,InsertNewFlag这个存储过程工作,检查指定的条件记录是否存在upadte表有多个(If..Else If),如果不存在那个表插入查询执行。
这里我包含了这个存储过程代码:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[InsertNewFlag]
(
@IsRead bit = NULL,
@IsImportant bit = NULL,
@IsTrashed bit = NULL,
@IsRemoved bit = NULL,
@User_id int,
@Post_History_id int,
@NewID int output
)
AS
BEGIN
IF EXISTS(SELECT * FROM [FileSystem].[dbo].[tbl_Post_History_Status] WHERE(User_id=@User_id AND Post_History_id=@Post_History_id))
BEGIN
IF @IsRead <> NULL
BEGIN
UPDATE [FileSystem].[dbo].[tbl_Post_History_Status]
SET [IsRead] = @IsRead
,[User_id] = @User_id
,[Post_History_id] = @Post_History_id
WHERE (User_id=@User_id AND Post_History_id=@Post_History_id)
SET @NewID = CAST(SCOPE_IDENTITY() AS INT)
END
ELSE IF @IsImportant <> NULL
BEGIN
UPDATE [FileSystem].[dbo].[tbl_Post_History_Status]
SET [IsImportant] = @IsImportant
,[User_id] = @User_id
,[Post_History_id] = @Post_History_id
WHERE (User_id=@User_id AND Post_History_id=@Post_History_id)
SET @NewID = CAST(SCOPE_IDENTITY() AS INT)
END
ELSE IF @IsTrashed <> NULL
BEGIN
UPDATE [FileSystem].[dbo].[tbl_Post_History_Status]
SET [IsTrashed] = @IsTrashed
,[User_id] = @User_id
,[Post_History_id] = @Post_History_id
WHERE (User_id=@User_id AND Post_History_id=@Post_History_id)
SET @NewID = CAST(SCOPE_IDENTITY() AS INT)
END
ELSE IF @IsRemoved <> NULL
BEGIN
UPDATE [FileSystem].[dbo].[tbl_Post_History_Status]
SET [IsRemoved] = @IsRemoved
,[User_id] = @User_id
,[Post_History_id] = @Post_History_id
WHERE (User_id=@User_id AND Post_History_id=@Post_History_id)
SET @NewID = CAST(SCOPE_IDENTITY() AS INT)
END
END
ELSE
BEGIN
INSERT INTO [FileSystem].[dbo].[tbl_Post_History_Status]
([IsRead]
,[IsImportant]
,[IsTrashed]
,[IsRemoved]
,[User_id]
,[Post_History_id])
VALUES
(@IsRead,@IsImportant,@IsTrashed,@IsRemoved,@User_id,@Post_History_id)
SET @NewID = CAST(SCOPE_IDENTITY() AS INT)
END
END
这个存储过程在语法上是否正确,并显示“命令已成功完成”等消息。但是当我执行这个存储过程并将值@IsRead传递给True并将@User_id传递给1而将@Post_History_id传递给9时,如果存在则记录不会更新。
记录如何存在。
请帮帮我..
答案 0 :(得分:1)
将所有空检查替换为此类
IF Not @IsRead Is Null --Instead of IF @IsRead <> NULL
BEGIN
.....Other CODE
END
ELSE IF Not @IsImportant Is Null --Instead of ELSE IF @IsImportant <> NULL
BEGIN
.....and SO ON
答案 1 :(得分:1)
试试这个,
UPDATE [FileSystem].[dbo].[tbl_Post_History_Status]
SET
[IsRead] =(CASE WHEN NOT @IsRead IS NULL THEN @IsRead ELSE [IsRead] END),
[IsImportant] =(CASE WHEN NOT @IsImportant IS NULL THEN @IsImportant ELSE [IsImportant] END),
[IsTrashed] =(CASE WHEN NOT @IsTrashed IS NULL THEN @IsTrashed ELSE [IsTrashed] END),
[IsRemoved] =(CASE WHEN NOT @IsRemoved IS NULL THEN @IsRemoved ELSE [IsRemoved] END),
[User_id] = @User_id,
[Post_History_id] =@Post_History_id
WHERE (User_id=@User_id AND Post_History_id=@Post_History_id)
答案 2 :(得分:0)
尝试
ALTER PROCEDURE [dbo].[InsertNewFlag]
(
@IsRead bit = NULL,
@IsImportant bit = NULL,
@IsTrashed bit = NULL,
@IsRemoved bit = NULL,
@User_id int,
@Post_History_id int,
@NewID int output
)
AS
BEGIN
Declare @Cnt Int
SET @Cnt = 0
SELECT Top 1 @Cnt = ID FROM [FileSystem].[dbo].[tbl_Post_History_Status] WHERE(User_id=@User_id AND Post_History_id=@Post_History_id)
IF @Cnt > 0
BEGIN
SET @NewID = @Cnt
IF Not @IsRead Is Null
BEGIN
UPDATE [FileSystem].[dbo].[tbl_Post_History_Status]
SET [IsRead] = @IsRead
,[User_id] = @User_id
,[Post_History_id] = @Post_History_id
WHERE (User_id=@User_id AND Post_History_id=@Post_History_id)
--SET @NewID = CAST(SCOPE_IDENTITY() AS INT)
END
ELSE IF Not @IsImportant Is Null
BEGIN
UPDATE [FileSystem].[dbo].[tbl_Post_History_Status]
SET [IsImportant] = @IsImportant
,[User_id] = @User_id
,[Post_History_id] = @Post_History_id
WHERE (User_id=@User_id AND Post_History_id=@Post_History_id)
--SET @NewID = CAST(SCOPE_IDENTITY() AS INT)
END
ELSE IF Not @IsTrashed Is Null
BEGIN
UPDATE [FileSystem].[dbo].[tbl_Post_History_Status]
SET [IsTrashed] = @IsTrashed
,[User_id] = @User_id
,[Post_History_id] = @Post_History_id
WHERE (User_id=@User_id AND Post_History_id=@Post_History_id)
--SET @NewID = CAST(SCOPE_IDENTITY() AS INT)
END
ELSE IF Not @IsRemoved Is Null
BEGIN
UPDATE [FileSystem].[dbo].[tbl_Post_History_Status]
SET [IsRemoved] = @IsRemoved
,[User_id] = @User_id
,[Post_History_id] = @Post_History_id
WHERE (User_id=@User_id AND Post_History_id=@Post_History_id)
--SET @NewID = CAST(SCOPE_IDENTITY() AS INT)
END
END
ELSE
BEGIN
INSERT INTO [FileSystem].[dbo].[tbl_Post_History_Status]
([IsRead]
,[IsImportant]
,[IsTrashed]
,[IsRemoved]
,[User_id]
,[Post_History_id])
VALUES
(@IsRead,@IsImportant,@IsTrashed,@IsRemoved,@User_id,@Post_History_id)
--SET @NewID = CAST(SCOPE_IDENTITY() AS INT)
SET @NewID = @@IDENTITY
END
END