declare @Bddr table
(
AddrId varchar(100)
);
With RootNodeCTE(Id,ParentId)
As
(
Select AddrId,AddrParent From gs_AddressInfo Where AddrParent In (@AddrID)
Union All
Select gs_AddressInfo.AddrId,gs_AddressInfo.AddrParent From RootNodeCTE
Inner Join gs_AddressInfo
On RootNodeCTE.Id = gs_AddressInfo.AddrParent
)insert into #Bddr (AddrId) select Id from RootNodeCTE where Id not in(select ParentId from RootNodeCTE)
;