Click here to Skip to main content
15,905,566 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
hello Everyone,
i have a problem in my stored procedure

SQL
insert into tbl_level(Parentid,childid,rewardpoint,earnedmoney,noofchild) values(@parentkey,@CusId,1,convert(decimal,(@BookingAmt/10)),1)
if exists (select 1 from tbl_level where childid=@parentkey)
begin
update tbl_level set earnedmoney= earnedmoney+@earnedmoney where parentid=(select Parentid from tbl_level where Childid=@parentkey)
     end

table has parentid and childid and earnedmoney columns now there is functionality when any record get inserted it's parent get commission , i want its parent's parents get commission upto 4 level.
e.g A has a child B and B has child C and C has child D so i want when D become parent of Child E commission amount should be go to D,C,B,A respectively.
Posted
Updated 13-Dec-13 20:10pm
v3
Comments
Then select one by one as you have did and update. Where is the problem?
Mohd Arif Khan 14-Dec-13 2:07am    
sir upto one level its fine but how to get select its upper level and update it
Try my answer below.
Maciej Los 15-Dec-13 17:08pm    
Please, provide more details about table structure and sample data...
Mohd Arif Khan 16-Dec-13 0:29am    
tbl_level
parentid childid earnedmoney
cus_1 cus_2 5000
cus_2 cus_3 5000
cus_3 cus_4 5000

i want when cus_4 becomes parent of node cus_5 the parent of cus_3 get commissiom and cus_2 and this gone upto 4 level how to perform it.

Tadit Dash Solution Is Good But What Happened When You Have Excessive Number Of Parents/Childs.
One Solution Is To Use While Loop With exists keyword instead of using if
like

SQL
Declare @nextParent int;
 
insert into tbl_level(Parentid,childid,rewardpoint,earnedmoney,noofchild) values(@parentkey,@CusId,1,convert(decimal,(@BookingAmt/10)),1)
while exists (select @nextParent=childid from tbl_level where childid=@parentkey)
begin
         update tbl_level set earnedmoney= earnedmoney+@earnedmoney where parentid=(select Parentid from tbl_level where Childid=@parentkey)
end
 
Share this answer
 
Comments
Mohd Arif Khan 14-Dec-13 4:21am    
select @nextParent=childid from tbl_level where childid=@parentkey this line is getting error of Incorrect syntax near '='.
try this

C#
if exists (select 1 from tbl_level where childid=@parentkey)
begin
insert into tbl_level(Parentid,childid,rewardpoint,earnedmoney,noofchild) values(@parentkey,@CusId,1,convert(decimal,(@BookingAmt/10)),1)
update tbl_level set earnedmoney= earnedmoney+@earnedmoney where parentid=(select Parentid from tbl_level where Childid=@parentkey)
     end
 
Share this answer
 
Comments
Mohd Arif Khan 14-Dec-13 5:37am    
@Joy Please mention what you have updated in existing code. and insert statement is needed to be executed one time only.
Try something like below.
SQL
Declare @nextParent int;

insert into tbl_level(Parentid,childid,rewardpoint,earnedmoney,noofchild) values(@parentkey,@CusId,1,convert(decimal,(@BookingAmt/10)),1)
if exists (select 1 from tbl_level where childid=@parentkey)
begin
         select @nextParent=childid from tbl_level where childid=@parentkey
 
         update tbl_level set earnedmoney= earnedmoney+@earnedmoney where parentid=(select Parentid from tbl_level where Childid=@parentkey)

         if exists (select 1 from tbl_level where childid=@nextParent)
         begin
                 select @nextParent=childid from tbl_level where childid=@nextParent

                  update tbl_level set earnedmoney= earnedmoney+@earnedmoney where parentid=(select Parentid from tbl_level where Childid=@nextParent)
         end
         
         -- Continue this flow...
end
 
Share this answer
 
v2
Comments
Mohd Arif Khan 14-Dec-13 3:45am    
it's getting error Incorrect syntax near '='. on select @nextParent=childid from tbl_level where childid=@parentkey line, and childid column is varchar type and you have matched it with in select statement how it will matched ever
I have updated the answer. Try now.
Mohd Arif Khan 16-Dec-13 5:06am    
hello Tadit, i'hv applied it but it's just updating upto one level
As I said "continue the flow"... That means do the same thing for next levels.
Do you understand now?
Mohd Arif Khan 16-Dec-13 5:51am    
i understand and did the same brother but it's not going to update the upper level.

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