T-SQL for Function

I am updating from SQL 2005 to SQL Server 2012 R2.

I have an old function that apparently isn't compatible with SQL Server 2012.

What do I need to do to the below code, to make it work in SQL 2012?

create function [dbo].[getChildren](@parent varchar(25))
returns table as return
with result(ItemChild) as
(select ItemChild from AlereBillMaterial where ItemParent=@parent
 union all
 select t.ItemChild from result r
 inner join AlereBillMaterial t on t.ItemParent = r.Itemchild)
select * from result
yonbretAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Try the following:
create function [dbo].[getChildren](@parent varchar(25))
returns table as return
(
   with result(ItemChild) as  
     (select ItemChild from AlereBillMaterial where ItemParent=@parent
      union all
      select t.ItemChild from result r
      inner join AlereBillMaterial t on t.ItemParent = r.Itemchild
    )
   select * from result
)

Open in new window

0
Brian CroweDatabase AdministratorCommented:
Let me know if you get any syntax errors...

CREATE FUNCTION [dbo].[getChildren]
(
	@parent VARCHAR(25)
)
RETURNS @Children TABLE
(
	ItemChild	VARCHAR(25) --assuming the datatype, change if necessary
)
BEGIN

	INSERT INTO @Children (ItemChild)
	SELECT ItemChild
	FROM AlereBillMaterial
	WHERE ItemParent = @parent
	UNION ALL
	SELECT t.ItemChild
	FROM result r
	INNER JOIN AlereBillMaterial t
		on t.ItemParent = r.Itemchild

	RETURN
END

Open in new window

0
Aneesh RetnakaranDatabase AdministratorCommented:
Its working for me on sql 2012
0
Russell FoxDatabase DeveloperCommented:
It looks okay to me, though it might be choking because you're calling your recursive CTE "result" which is a SQL reserved word. Try just changing that to something else, like MyResult:
CREATE FUNCTION [dbo].[getChildren](@parent VARCHAR(25))
	RETURNS TABLE 
AS 
RETURN

WITH MyResult(ItemChild) AS 
	(
		SELECT ItemChild FROM AlereBillMaterial WHERE ItemParent=@parent
		UNION ALL 
		SELECT t.ItemChild FROM result r INNER JOIN AlereBillMaterial t ON t.ItemParent = r.Itemchild
	)

SELECT * FROM MyResult

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yonbretAuthor Commented:
Once I changed result to MyResult, it worked fine.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.