I have a working CTE in a stored procedure that I need to turn into a function. From research I know its possible but I'm having a heck of a time figuring out how to do it. Can you help?The plan is to insert an employee id (an email address) and the level of manager you want (A=ceo, b=vice president, c=regional manager, d=supervisor, etc). To run the current sp you enter this:exec usp_findmanager 'craig@hotmail.com','b'And you are returned with the vice president craig reports to.Here is the stored procedure that runs this CTE:USE [IDM_V]GO/****** Object: StoredProcedure [dbo].[usp_findmanager] Script Date: 02/04/2010 10:22:05 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Create date: February 1, 2010-- Description: Takes two inputs, employee UPN (email address) and -- the letter representing the manager level you want to see. -- For example enter: ---- exec usp_findmanager 'anthony@hotmail.com', 'C'-- -- to find the manager at level C for Anthony.-- =============================================ALTER PROCEDURE [dbo].[usp_findmanager] -- Add the parameters for the stored procedure here @upn varchar(100), @empgroup varchar (3)ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure hereWITH ORG (managerUPN,userprincipalName,[Level], employeegroup) AS(SELECT anchor.managerUPN,anchor.userprincipalName,1, employeegroup FROM IDM_Identities anchor WHERE anchor.userprincipalName = @upn UNION ALL SELECT e.managerUPN, e.userprincipalName,Level +1, e.employeegroup FROM IDM_Identities e JOIN ORG o on e.userprincipalName = o.managerUPN collate SQL_Latin1_General_CP1_CI_AS)SELECT top 1 @upn as Employee, managerUPN as 'requested supervisor', employeegroup as LevelFROM orgWHERE employeegroup=@empgroupORDER BY LEVEL ASC --change from asc to desc to change priority levelEND
So how can I turn this into a working funtion instead of a SP?Craig Greenwood