if OBJECT_ID('fn_CSORGA_GetDCDEPT') is not null drop function fn_CSORGA_GetDCDEPT
go
create function fn_CSORGA_GetDCDEPT(@P2 uniqueidentifier)
returns @GetDCDEPT table(ODESC varchar(200),ODESCCHS varchar(200),VGUID uniqueidentifier)
as
Begin
with cte(ODESC,ODESCCHS,VGUID) as (
select
A.ODESC,
A.ODESCCHS,
A.VGUID
from csorga_1 A
where VGUID=@P2
union all
select
B.ODESC,
B.ODESCCHS,
B.VGUID
from csorga_1 B
inner join cte A on B.PGUID=A.VGUID
)
insert into @GetDCDEPT(ODESC,ODESCCHS,VGUID)
select distinct * from cte
return
end