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