SQL Server deletes a table's foreign key constraint

Source: Internet
Author: User

Many tables in SQL Server have foreign key constraints. before deleting these tables, you must first Delete these foreign key constraints.

The stored procedure used to delete the foreign key constraints of a table:

  1. Create procedure sp_drop_all_fk
  2. As
  3. Declare @ SQL varchar (255)
  4. Declare dropsql_cursor cursor
  5. Select 'alter table' + object_name (fkeyid) + 'drop constraint' + object_name (constid) + char (10) from sysreferences
  6. Open dropsql_cursor
  7. Fetch dropsql_cursor into @ SQL
  8. Begin tran
  9. While @ fetch_status = 0
  10. Begin
  11. Execute (@ SQL)
  12. If @ error <> 0
  13. Begin
  14. Rollback
  15. Return
  16. End
  17. Fetch dropsql_cursor into @ SQL
  18. End
  19. Deallocate dropsql_cursor
  20. Commit
  21. Go
  22. Exec sp_drop_all_fk -- executes the Stored Procedure
Related Article

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.