在存储过程中使用多个更新语句时,处理返回值的最佳方法是什么。
我在数据适配器中使用此存储过程,并尝试获取更新结果总数。
我正在使用 SQL Server 2008 和.NET 4.5
例如:
CREATE PROCEDURE [dbo].[usrsp_MessageConversation_delete]
(
@fromMbrID int,
@toMbrID int
)
AS
BEGIN
SET NOCOUNT OFF;
UPDATE mbr_Messages
SET fromDeleted = 1
WHERE (fromMbrID = @fromMbrID) AND (toMbrID = @toMbrID)
UPDATE mbr_Messages
SET toDeleted = 1
WHERE (fromMbrID = @toMbrID) AND (toMbrID = @fromMbrID)
//RETURN TOTAL NUMBER OF UPDATED RECORDS
EG: RETURN @@ROWCOUNT ??
END
我想返回更新记录的总数,如果未更新则返回0。
我是否需要添加任何其他错误处理 - 如果有任何建议?
更新:我最终选择了这个:
BEGIN
SET NOCOUNT ON;
DECLARE @rowCounter INT;
BEGIN TRY
UPDATE
mbr_Messages
SET
fromDeleted = 1
WHERE
(fromMbrID = @fromMbrID) AND (toMbrID = @toMbrID) AND (fromDeleted = 0);
SET @rowCounter = @@ROWCOUNT;
UPDATE
mbr_Messages
SET
toDeleted = 1
WHERE
(fromMbrID = @toMbrID) AND (toMbrID = @fromMbrID) AND (toDeleted = 0);
SET @rowCounter = @rowCounter + @@ROWCOUNT;
END TRY
BEGIN CATCH
PRINT 'Update failed';
END CATCH
RETURN @rowCounter
END
答案 0 :(得分:1)
如果你的存储过程很短,我真的不建议使用任何错误处理。
但这是错误处理的一个例子
IF @@ERROR <> 0
BEGIN
--your statement
return 12345; -- to mark your error location
END
有关@@Error
的更多信息答案 1 :(得分:1)
在程序开始附近使用事务使用此命令启动事务:
BEGIN TRANSACTION
检查脚本中的错误(可以多次使用)
IF @@ERROR <> 0
BEGIN
ROLLBACK
RAISERROR ('error updating something', 16,1)
RETURN
END
然后在脚本结束时提交事务
COMMIT
答案 2 :(得分:1)
您想要执行以下操作:
CREATE PROCEDURE [dbo].[usrsp_MessageConversation_delete]
(
@fromMbrID int,
@toMbrID int
)
AS
BEGIN
SET NOCOUNT OFF;
DECLARE @RowCount1 INTEGER
DECLARE @RowCount2 INTEGER
DECLARE @TotalRows INTEGER
UPDATE mbr_Messages
SET fromDeleted = 1
WHERE (fromMbrID = @fromMbrID) AND (toMbrID = @toMbrID)
SET @RowCount1=@@RowCount
UPDATE mbr_Messages
SET toDeleted = 1
WHERE (fromMbrID = @toMbrID) AND (toMbrID = @fromMbrID)
SET @RowCount2=@@RowCount
SET @TotalRows = @RowCount1 + @RowCount2
--RETURN TOTAL NUMBER OF UPDATED RECORDS
RETURN @TotalRows
END
您需要将@@ RowCount分配给某个变量,因为它在您使用后会重置。
修改强>
还添加错误处理代码:Try..Catch and Transactions。
答案 3 :(得分:0)
您可以使用OUTPUT子句来计算更新/插入或删除的记录
DECLARE @MyTableVar table (
flagdeleted bit NOT NULL);
UPDATE mbr_Messages
SET fromDeleted = 1
OUTPUT INSERTED.fromDeleted INTO @MyTableVar
WHERE (fromMbrID = @fromMbrID) AND (toMbrID = @toMbrID)
UPDATE mbr_Messages
SET toDeleted = 1
OUTPUT INSERTED.toDeleted INTO @MyTableVar
WHERE (fromMbrID = @toMbrID) AND (toMbrID = @fromMbrID)
SELECT COUNT(*) FROM @MyTableVar //here is the number of updated records
答案 4 :(得分:0)
CREATE PROCEDURE [dbo].[usrsp_MessageConversation_delete] ( @fromMbrID int, @toMbrID int ) AS DECLARE @SQL1 VARCHAR(800), @SQL2 VARCHAR(800) BEGIN SET NOCOUNT OFF; SET @SQL1 =' UPDATE mbr_Messages SET fromDeleted = ''1'' WHERE (fromMbrID = '+@fromMbrID+') AND (toMbrID = '+@toMbrID+')' PRINT @SQL1 SET @SQL2 =' UPDATE mbr_Messages SET toDeleted = ''1'' WHERE (fromMbrID = '+@toMbrID+') AND (toMbrID = '+@fromMbrID+')' PRINT @sql2 EXEC(@SQL1) EXEC(@SQL2) END
答案 5 :(得分:0)
试试这个
BEGIN TRY
BEGIN TRAN
-- Your code here
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
这将返回受影响的行数(更新的行)。如果错误,则0(回滚后受影响的行将为0)。
答案 6 :(得分:0)
SqlCommand.ExecuteNonQuery
的返回值将为您提供受查询影响的行数。在您的情况下,它将在您的proc中添加两个UPDATE
语句的行数。无需向存储过程添加RETURN语句。
SqlCommand command;
...
int numberOfRecords = command.ExecuteNonQuery();