Delete SQL stored procedures for infinitus!

Source: Internet
Author: User
Create procedure [DBO]. [category_delete] @ categoryid int as -- delete [category] -- where categoryid = @ categoryid -- delete [category] -- where fatherid = @ categoryid declare @ Taba table ([Tabid] [int] not null) insert @ Taba (Tabid) Select categoryid from category where fatherid = @ categoryiddeclare @ ID intwhile (exists (select top 1 * From @ Taba )) -- as long as there is data, continue to loop begin select top 1 @ ID = Tabid from @ Taba -- retrieve a record from the variable table insert @ Taba (Tabid) -- Insert the sub-record with the parent ID equal to @ ID into the record select categoryid from category where fatherid = @ ID Delete from category where categoryid = @ ID -- delete the record from the original table delete from @ Taba where tabid = @ ID -- delete the record from the variable table, because it is unnecessary for end Delete [category] Where categoryid = @ categoryid to retrieve all its subrecords. This segment can be used in an infinitely hierarchical table.

Table Design:

 

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.