Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sql Transaction - Not showing desired result

Author  Topic 

sharpcnet
Starting Member

8 Posts

Posted - 2013-12-21 : 06:50:03
tblParent

pid(int) name deleted(bit)
1 abc 0
2 def 0

tblChild

cid(int) name pid(ForeignKey)
1 aaa 1
2 bbb 1

When a record from tblParent is being deleted, it should check for any child records. If yes, rollback & return 0. If no, then update the deleted column to '1' and return 1. Basically, doing a soft delete

The SP works fine. All I need is to know the status as 0 or 1 based upon the action that took place. How should it be done. I would call this store procedure from c#, linq to entities to get the status. something like:

public int somefuntion() //returning a string is also fine..
{
return MYDB.SoftDelete(parameters.....);
}


ALTER PROCEDURE SoftDelete
(
@TableName nvarchar(50), @ColName nvarchar(50),
@Id nvarchar(50)
)
AS
BEGIN
DECLARE @qry nvarchar(500)

SELECT @qry = 'begin transaction
delete '+@tablename+' where '+@colname+'='+@id+'
if(@@Error <> 0)
Begin
--select 0
End
else
Begin
rollback transaction
update '+@tablename+' set deleted = 1 where '+@colname+' = '+@id+'
--select = 1
end'

EXECUTE sp_executesql @qry
END

Please also note that, there could be several child tables using pid as FK.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-21 : 11:59:42
just use RETURN statement to return the status bit. something like

ALTER PROCEDURE SoftDelete
(
@TableName nvarchar(50), @ColName nvarchar(50),
@Id nvarchar(50)
)
AS
BEGIN
DECLARE @qry nvarchar(500)

SELECT @qry = 'begin transaction
delete '+@tablename+' where '+@colname+'='+@id+'
if(@@Error <> 0)
Begin
return 0
End
else
Begin
rollback transaction
update '+@tablename+' set deleted = 1 where '+@colname+' = '+@id+'
return 1
end'

EXECUTE sp_executesql @qry
END

Didnt understand need of dynamic sql here though. Why is your table changing?

also you should call sp like below


DECLARE @Ret int
EXEC @ret = SoftDelete ....
--the below will give you return status
SELECT @Ret


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sharpcnet
Starting Member

8 Posts

Posted - 2013-12-22 : 01:03:47
Just because the same function can be used for any table.

The DECLARE @Ret comes after the 'END'..right. It gave these errors:

A return statement with a return value cannot be used in this context.
Maximum stored procedure, function, trigger, or view nesting level exceeded(limit 32).
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-22 : 03:07:32
quote:
Originally posted by sharpcnet

Just because the same function can be used for any table.

The DECLARE @Ret comes after the 'END'..right. It gave these errors:

A return statement with a return value cannot be used in this context.
Maximum stored procedure, function, trigger, or view nesting level exceeded(limit 32).


Nope DECLARE @Ret part is not with stored procedure

its to be used in place where you invoke the stored procedure (ie from application)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sharpcnet
Starting Member

8 Posts

Posted - 2013-12-22 : 03:22:53
Thanks for confirming. Tried as you said. It gives 2 errors - line 5, 11
A return statement with a return value cannot be used in this context.

Maybe its because thats a dynamic sql and cant return a value there.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-22 : 13:24:42
why do you need dynamic sql? I dont see any reason for this. You should not write procedures generalised like this. There should be separate DML procedures for each main entities (tables)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -