SQL Server removes constraints for table fields and field

Source: Internet
Author: User

When you delete a field from a database table, you use the ALTER TABLE table name drop column name

The error returned by the server is:
Server:msg 5074, Level A, State 1, line 1
The object constraint name is a dependent on column name.
Server:msg 4922, Level A, State 1, line 1
ALTER TABLE DROP column name failed because one or more objects access the This column.

The workaround is to remove all constraints on this column first, and then start deleting the column

The following code removes the Name column from table B_file.

DECLARE  @sqlCmd nvarchar(1024x768);DECLARE  @tbname nvarchar( the),--name of table to be processed        @fdname nvarchar( the),--the name of the field to process        @delfield bit=1  --0 Delete a relationship only, 1 delete the field at the same timeSet @tbname='B_file';Set  @fdname= 'Name'; BEGIN   --defines a cursor.   DECLAREC_test_mainCURSORFast_forward for           --Default value constraint        SelectSql='ALTER TABLE ['+B.name+'] Drop constraint ['+D.name+']'         fromsyscolumns aJoinSysobjects b ona.ID=b.ID andA.name=@fdname  andB.name=@tbname          Joinsyscomments C onA.cdefault=c.idJoinsysobjects D onC.id=d.idUnion --FOREIGN Key Reference        SelectS='ALTER TABLE ['+C.name+'] Drop constraint ['+B.name+']'         fromSysforeignkeys aJoinSysobjects b onb.ID=A.constidJoinsysobjects C onC.id=A.fkeyidJoinSyscolumns D onD.id=C.id andA.fkey=D.colid andD.name=@fdname         Joinsysobjects E onE.id=A.rkeyid andE.name=@tbname         Joinsyscolumns F onF.id=E.id andA.rkey=F.colidUnion --primary key/Unique key/index        Select  Case  whenE.xtypeinch('PK','UQ') Then 'ALTER TABLE ['+C.name+'] Drop constraint ['+E.name+']'          Else 'DROP INDEX ['+C.name+']. ['+A.name+']' End         fromsysindexes aJoinSysindexkeys b ona.ID=b.ID andA.indid=B.indidJoinsysobjects C onb.ID=C.id andC.xtype='U'  andC.name=@tbname         JoinSyscolumns D onb.ID=D.id andB.colid=D.colid andD.name=@fdname          Left Joinsysobjects E onE.id=object_id(A.name)whereA.indid not inch(0,255)        --open the cursor.   OPENC_test_main; --populate the data.   FETCH NEXT  fromC_test_main into @sqlCmd; --If the data is retrieved, it is processed.    while @ @fetch_status = 0   BEGIN     PRINT @sqlCmd; execsp_executesql@sqlcmd;--perform a delete operation on a constraint     --fills the next piece of data.     FETCH NEXT  fromC_test_main into @sqlCmd; END; --Close Cursors   CLOSEC_test_main; --releases the cursor.   deallocateC_test_main;END; if @delfield=1  begin --determine if a table exists --if exists (select 1 from sys.tables where [email protected] and type = ' u ') --determine if a column exists IF col_length(@tbname,@fdname) is  not NULL exec('ALTER TABLE ['+@tbname+'] Drop Column ['+@fdname+']') End

SQL Server removes constraints for table fields and field

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.