Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stored Procedure For A Soft Delete


Stored Procedure For A Soft Delete

Author
Message
sharpcnet
sharpcnet
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 12
This code is to perform a Soft Delete. 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 @Param = '@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#SadLinq To Entities)

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


Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24176 Visits: 37946
sharpcnet (12/22/2013)
This code is to perform a Soft Delete. 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

SET @qryDel = 'delete from'+@tablename+' where '+@colname+'='+@id
SET @qryUpd = 'update '+@tablename+' set deleted = 1 where '+@colname+' = '+@id

BEGIN TRY
BEGIN TRANSACTION
EXECUTE sp_executesql @qryDel
ROLLBACK TRANSACTION
BEGIN TRANSACTION
EXECUTE sp_executesql @qryUpd
COMMIT TRANSACTION
SET @Status = 1
END TRY

BEGIN CATCH
ROLLBACK TRANSACTION
SET @Status = 0
END CATCH

SELECT @Status

END


C#SadLinq To Entities)

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





Try changing SELECT @Status to RETURN @Status

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
sharpcnet
sharpcnet
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 12
Should I have an output parameter then? I declared one.. @Status int out.
When I execute the SP from SS2008, it gives two results - @Status - 0 ; Return Value - 0. - For every record.

Updated my query . please have a look
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45004 Visits: 39880
Unfortunately, this is a duplicate post. No more replies on this thread, please. Unless I missed my guess, this problem has been solved at the following thread.
http://www.sqlservercentral.com/Forums/Topic1525325-392-1.aspx

@sharpcnet,
Please don't double post. It just splits up people's thoughts and possible answers. Most of the heavy hitters monitor all posts no matter which forum they're in so no need to post on multiple forums ,never mind posting the same question on the same forum more than once.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search