SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sql Transaction - Not showing desired result
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sharpcnet
Starting Member

8 Posts

Posted - 12/21/2013 :  06:50:03  Show Profile  Reply with Quote
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.

Edited by - sharpcnet on 12/21/2013 07:48:34

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 12/21/2013 :  11:59:42  Show Profile  Reply with Quote
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 - 12/22/2013 :  01:03:47  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 12/22/2013 :  03:07:32  Show Profile  Reply with Quote
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 - 12/22/2013 :  03:22:53  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 12/22/2013 :  13:24:42  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000