Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi please help me to create a procedure in which there are two tables one is parent table and other is child table. so if i delete content from parent table then while deleting it should first delete the records associated with child table first and then the parent table. in procedure i have to use begin transaction, rollback and commit as if one of the records from child table if it is in edit mode then while deleting the records can not b deleted as it should be rollback if its not in edit mode then it should b commit.

and please help me how to test the if it is in edited mode.. thanks in advance...
Posted
Updated 8-Mar-12 17:24pm
v3

Rather than creating a procedure, I'd suggest you create a trigger on parent's table. AFAIK, that's the most trusted way to ensure that the child rows would be deleted whenever the parent row is deleted. And it respect transaction by default :)

hmm.... Okay. Now, you wrote:
i made an sp that works delete both parent and child records i want but now the problem is testing the sp as i m unable to lock records lets consider a scenario in which record from parent contains three records in child now if i lock one of the recorfd then the delete transaction shud b rollback so in this case how to lock one of the row from child record please help me for that.

So, as far as I understand (please cmiiw), you already created the SP, but you want the SP to cancel the deletion if any of its child is locked or being edited. Right? Ok. How do you 'lock' the child row? I think it's better if you add one column on the child -- say, IsLocked (bit) -- and set it to 1 if the row is 'in edit mode'. If the the apps already not in the edit mode, then the apps should update the bit to 0. On the delete SP, you can check if there is/are any child that has IsLocked turn on. If it is, then skip the deletion. If not (all child's IsLocked is off) then the SP can do the deletion. I mean something like this:

SQL
If not exists (Select * from child where IsLocked = 1)
   --- not exists; then delete the parent and the child:
   Begin
       delete from parent
        where key_col = @Key_of_row_tobe_deleted
    
       delete from child
        where foreign_key = @Key_of_row_tobe_deleted
   End

--- otherwise, delete neither, in other words skip the deletion



hth,
foxyland
 
Share this answer
 
v2
Comments
vins555 8-Mar-12 23:31pm    
okay thanks i wl try that.
vins555 9-Mar-12 0:29am    
is it possible with stored procedure???
vins555 9-Mar-12 1:07am    
i made an sp that works delete both parent and child records i want but now the problem is testing the sp as i m unable to lock records lets consider a scenario in which record from parent contains three records in child now if i lock one of the recorfd then the delete transaction shud b rollback so in this case how to lock one of the row from child record please help me for that.
USe cascading delete .if a row in parent table is deleted ,then autoamtically row belonging to child table deleted.
 
Share this answer
 
Comments
vins555 9-Mar-12 1:06am    
i made an sp that works delete both parent and child records i want but now the problem is testing the sp as i m unable to lock records lets consider a scenario in which record from parent contains three records in child now if i lock one of the recorfd then the delete transaction shud b rollback so in this case how to lock one of the row from child record please help me for that.
Hay vins555
you have the option to delete the child record when parent record is deleted
It simple while giving relation between two table you give the relation as cascade That's all when u delete the parent record it automatically deletes the child record


Hope it will help you.....
 
Share this answer
 
if your Primary key is set to the FK in child table then you can simply set delete rule to cascade. This will delete all child data before deleting from main table.Hope this will help.
 
Share this answer
 
Table1 is a master table and table2 is a child table.

Table1 definition
t1column1 int (pk)
t1column2 varchar(50)

Table1 definition
t2column1 int (pk)
t1column1 int (fk)

Delete query
SQL
delete from table2 where t1column1 = 1
delete from table1 where t1column1 = 1
 
Share this answer
 
Comments
vins555 9-Mar-12 1:07am    
i made an sp that works delete both parent and child records i want but now the problem is testing the sp as i m unable to lock records lets consider a scenario in which record from parent contains three records in child now if i lock one of the recorfd then the delete transaction shud b rollback so in this case how to lock one of the row from child record please help me for that.
Mukund Thakker 9-Mar-12 7:19am    
please list your parent, child table definition so that I can provide your proper solution. In general case the above solution will definitely works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900