MYSQL gets the current node and recursively all ancestor nodes

Source: Internet
Author: User

--MYSQL Gets the current node and recursively all ancestor nodes--parameter description: Resultfield: Query return field, IDD to query the resource ID value, idfieldname ID field name, parentidfieldname ancestor ID field name, TableName table name, whether iscontainmyself contains its own drop procedure if EXISTS pro_getparentlist;create procedure pro_getparentlist (in Resultfield varchar (4000), in myID varchar (+), in idfieldname varchar (+), in parentidfieldname varchar (+), in TableName varchar (+), iscontainmyself int) begindrop table if exists tmpparent;  CREATE TABLE tmpparent (temid varchar); set @tempParentId =myid; Set @myDoflag = 1; While @myDoflag >0 does set @ssss = CONCAT (' Select ', Parentidfieldname, ', COUNT (*) into @tempParentId, @myDoflag F    Rom ', TableName, ' where ', idfieldname, ' = ', ' ', @tempParentId, ' ');p repare strSQL from @ssss; execute strSQL;         deallocate prepare strSQL;  if (@myDoflag >0) then sets @insertSql = CONCAT (' INSERT into tmpparent (temid) VALUES (', ' ' ", @tempParentId," ' ", ') ');  Prepare preSql1 from @insertSql;    Execute PRESQL1;     deallocate prepare PRESQL1;  End If;end while; IF (iscontainmyself=1)  Theninsert into Tmpparent (temid) values (myID); END IF; Set @fieldSql =concat (' SELECT ', Resultfield), set @strsql = Concat (@fieldSql, ' from ', TableName, ' s ', ' inner join Tmppar Ent t on T.temid=s ", Idfieldname, ' order by S. ', Idfieldname, ' ASC ');p repare presql from @strsql; execute presql; End

  

MYSQL gets the current node and recursively all ancestor nodes

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.