Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to alter length of the column for a table with pk constraints and fk constraints in Sql server script wise?


SQL
ALTER TABLE NON_MOL_Corporate
ALTER COLUMN [CorporateId] varchar(50)



Getting below error
C#
The object 'PK_Non-MOL_Corporate' is dependent on column 'CorporateId'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN CorporateId failed because one or more objects access this column.


What I have tried:

I tried as below:
SQL
ALTER TABLE NON_MOL_Corporate
ALTER COLUMN [CorporateId] varchar(50)



Getting below error
C#
The object 'PK_Non-MOL_Corporate' is dependent on column 'CorporateId'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN CorporateId failed because one or more objects access this column.
Posted
Updated 15-Mar-16 19:20pm

1 solution

Hello ,
This error comes if there exists some foreign key constraints . To avoid this issue , you first drop the foreign key constraints then alter the column and then create the foreign key constraint again .

Try this
ALTER TABLE [dbo.childtable] DROP CONSTRAINT [FK_Non-MOL_Corporate];

---do the other stuff 
ALTER TABLE NON_MOL_Corporate
ALTER COLUMN [CorporateId] varchar(50)

ALTER TABLE [dbo.childtable] ADD FOREIGN KEY (FK_Non-MOL_Corporate) 
    REFERENCES NON_MOL_Corporate(CorporateId);

Thanks
 
Share this answer
 
Comments
ranio 16-Mar-16 1:52am    
how to get the child table for the referenced Foreign Key Constraint?
ranio 16-Mar-16 3:31am    
when i tried to remove the constraint it shows it doesn't exists.
Animesh Datta 16-Mar-16 4:24am    
to get list of all foreign keys referencing a given table in SQL Server you can do this
EXEC sp_fkeys 'TableName'
and then check what it defines .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900