此代码用于执行软删除。它首先删除一条记录。如果删除成功,则回滚并更新同一记录的标志列。如果该记录具有依赖关系并且无法删除,则不执行任何操作。为了知道动作发生了,我保留了 Status 变量。但是,无论如何,它总是结果为空。我哪里错了。
ALTER PROCEDURE SoftDelete
(
@TableName nvarchar(50), @ColName nvarchar(50),
@Id nvarchar(50)
)
AS
BEGIN
DECLARE @qryDel nvarchar(MAX),@qryUpd nvarchar(MAX),@Status int = null,
@Param nvarchar(MAX)
SET @Param = N'@TableName nvarchar(50), @ColName nvarchar(50),
@Id nvarchar(50)'
SET @qryDel = N'delete from @tablename where @colname=@id'
SET @qryUpd = N'update @tablename set deleted = 1 where @colname=@id'
BEGIN TRY
BEGIN TRANSACTION
EXECUTE sp_executesql @qryDel, @Param, @TableName, @ColName, @Id
ROLLBACK TRANSACTION
BEGIN TRANSACTION
EXECUTE sp_executesql @qryUpd, @Param, @TableName, @ColName, @Id
COMMIT TRANSACTION
SET @Status = 1
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
SET @Status = 0
END CATCH
SELECT @Status
END
C#:( Linq 到实体)
public int SoftDelete()
{
return MYDB.SoftDelete("tblCountry","CountryId,"101").FirstOrDefault ?? -1;
}