SQL Server Drop Talbe automatically deletes the associated foreign key, permission system

Source: Internet
Author: User

 if object_id('PROC_DROPTABLEWITHFK') is  not NULL beginDrop procdbo. PROC_DROPTABLEWITHFKEndGOCreate procProc_droptablewithfk@PK_tableName nvarchar( -) asbeginDeclareTest_curcursorLocal forSelectO2.name as 'Fk_name', O3.name as 'table_name'  fromSysforeignkeys FKInner Joinsys.objects O1 onFk.rkeyid=O1.object_id  Inner Joinsys.objects O2 onFk.constid=O2.object_idInner JoinSys.objects O3 onFk.fkeyid=O3.object_idwhereO1.name=@PK_tableNameOpenTest_curDeclare @FK_name nvarchar(255),@Table_Name nvarchar(255)Fetch Next  fromTest_cur into @FK_name,@Table_Name while @ @FETCH_STATUS=0begin    exec('ALTER TABLE'+@Table_Name+'DROP CONSTRAINT'+@FK_name)Fetch Next  fromTest_cur into @FK_name,@Table_NameEnd CloseTest_curdeallocateTest_curexec('drop table'+@PK_tableName) End  Goif object_id('Sys_user') is  not NULLexecDbo. Proc_droptablewithfk'Sys_user'GOCreate TableSys_user (Sys_useridint Identity(1,1)Primary Key, UserAccountnvarchar(255) not NULL, Userpwdnvarchar(255) not NULL, UserNamenvarchar(255) )Goif object_id('Sys_powerteam') is  not NULLexecDbo. Proc_droptablewithfk'Sys_powerteam'GoCreate TableSys_powerteam (Sys_powerteamidint Identity(1,1), Powerteamnamenvarchar(255) not NULL)Goif object_id('Sys_powerteamforuser') is  not NULLexecDbo. Proc_droptablewithfk'Sys_powerteamforuser'GoCreate TableSys_powerteamforuser (Sys_powerteamforuseridint Identity(1,1)Primary Key, Sys_powerteamidint, Sys_useridint )Goif object_id('Sys_menu') is  not NULLexecDbo. Proc_droptablewithfk'Sys_menu'GoCreate TableSys_menu (Sys_menuidint Identity(1,1)Primary Key, the Urlnvarchar(255), ParentIDint, MenuNamenvarchar(255) not NULL, Ordernumint )Goif object_id('Sys_powerteamformenu') is  not NULLexecDbo. Proc_droptablewithfk'Sys_powerteamformenu'GoCreate TableSys_powerteamformenu (Sys_powerteamformenuint Identity(1,1)Primary Key, Sys_powerteamidint, Sys_menuidint)Go

This is the establishment of a permission system table structure with no foreign key relationships.

PROC_DROPTABLEWITHFK This stored procedure to be deleted indicates that all of his foreign key tables and foreign key names will be queried, in which the sets are placed inside the cursor, all foreign keys are deleted, and the table is finally deleted.

Not to be continued ...

SQL Server Drop Talbe automatically deletes the associated foreign key, permission system

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.