Stored Procedure For A Soft Delete

Posted by Sharpcnet under Sql Server on 12/22/2013 | Points: 10 | Views : 699 | Status : [Member] | Replies : 0
This code is to perform a Soft Delete. (Not deleting a record but just flagging it).
It deletes a record first. If the delete was successful, rollsback and then updates the flag column of the same record. If that record has dependencies and could not be deleted, does nothing. In order to know, the action happened, I kept the Status variable.
But, it always results as null, no matter what. Where am I going wrong.

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 @patam = 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#

public int SoftDelete()
{
return MYDB.SoftDelete("tblCountry","CountryId,"101").FirstOrDefault ?? -1;
}





Responses

(No response found.)

Login to post response