Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
ALTER PROCEDURE [vainimarketing].[ppairincome]
@key varchar(50)
AS
BEGIN
DECLARE @parent Varchar(50)
DECLARE @lcount INT
DECLARE @pair INT
DECLARE @rcount INT
	SET NOCOUNT ON;
While(@key!='VA251000')
Begin
Select @parent=ParentId from Tree Where ChildId=@key;
Select @pair=TotalPair From Tree where ChildId=@parent; 

with cte as (
       select
               ChildId, ParentId, position,
               null lnode,
               null rnode
       from Tree where ChildId = @parent
       union all
       select
               t.ChildId, t.ParentId, t.position,
               ISNULL(cte.lnode, CASE WHEN t.position = 'LEFT' THEN 1 ELSE 0 END) lnode,
               ISNULL(cte.rnode, CASE WHEN t.position = 'RIGHT' THEN 1 ELSE 0 END) rnode
       from Tree t
       inner join cte
               on cte.ChildId = t.ParentId
)
select SUM(lnode)LeftNodes,SUM(rnode)RightNodes from cte;
set @key=@parent;
End
END

In this I would like to store the value of SUM(lnode)LeftNodes and SUM(rnode)RightNodes in @lcount and @rcount respectively.

Please help it is urgent ...

I need to do something like
SQL
select @lcount=SUM(lnode)LeftNodes,@rcount=SUM(rnode)RightNodes from cte;
Posted
Updated 19-Dec-12 21:06pm
v2

1 solution

Try changing null lnode to 0 lnode you cannot aggregate a null.
 
Share this answer
 

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