> Server: messaggio 5074, livello 16, stato 1, riga 1
> Il oggetto 'DF__MyTable__ProvR__733B0D96' dipende da colonna 'MyColumn'.
> Server: messaggio 4922, livello 16, stato 1, riga 1
> ALTER TABLE ALTER COLUMN MyColumn non è riuscita perché uno o più oggetti accedono a questa colonna.
Ciao ysdemarc,
l'errore è dovuto al fatto che sulla colonna ProVR della tabella MyTable è stato definito un constraint DEFAULT che assegna automaticamente un valore se in fase di INSERT non viene specificata la colonna oppure viene passato il valore DEFAULT.
Prima di poter modificare la definizione della colonna, occorre rimuovere tutti gli eventuali constraint definiti su di essa.
Inoltre sarebbe opportuno assegnare un nome significativo ad un constraint, diversamente SQL Server assegnerà automaticamente un nome autogenerato.
Guarda il seguente esempio:
USE tempdb;
GO
/* Definisco la tabella dbo.Students */
CREATE TABLE dbo.Students(
StudentID int NOT NULL IDENTITY CONSTRAINT PK_StudentID PRIMARY KEY,
FirstName varchar(10) NOT NULL,
LastName varchar(10) NOT NULL,
Age smallint NOT NULL DEFAULT 20
);
GO
/* Tento di modificare il data type della colonna Age */
ALTER TABLE dbo.Students
ALTER COLUMN Age int NOT NULL;
GO
/* Output:
Msg 5074, Level 16, State 1, Line 1
The object 'DF__Students__Age__0AD2A005' is dependent on column 'Age'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN Age failed because one or more objects access this column.
*/
/* Elimino il constraint DEFAULT */
ALTER TABLE dbo.Students
DROP CONSTRAINT DF__Students__Age__0AD2A005;
GO
/* Modifico il data type della colonna Age*/
ALTER TABLE dbo.Students
ALTER COLUMN Age int NOT NULL;
GO
/* Definisco un nuovo default */
ALTER TABLE dbo.Students
ADD CONSTRAINT DF_Age DEFAULT 25 FOR Age;
GO
/* Pulizia */
DROP TABLE dbo.Students;
Ciao!
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
http://italy.mvps.org