SQL recursive query subtype

Source: Internet
Author: User
If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [f_getchildcate] ') and xtype in (n'fn', n'if', n'tf '))
Drop function [DBO]. [f_getchildcate]
Go

Set quoted_identifier off
Go
Set ansi_nulls off
Go

Create Function f_getchildcate (@ id int)
Returns @ child table ([cateid] int, catename nvarchar (127), parentid INT)
As
Begin

Begin
Insert @ child select ID, c_catename, c_parentid from [t_cate] Where [c_parentid] = @ ID
While @ rowcount> 0
Begin
Insert @ child select B. ID, B .C _ catename, B .C _ parentid from @ child
Inner join [t_cate] B on A. [cateid] = B. [c_parentid]
Where B. [c_parentid] Not in (select distinct [parentid] From @ child)
End
End

Return
End

Go
Set quoted_identifier off
Go
Set ansi_nulls on
Go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.