.Net ramblings
# Wednesday, 10 September 2008
Alter a column with a default value
say you wanted to change a BIT column to a NVARCHAR column, and the BIT column has a default value set to 0 or 1.  you can't run the following statement or you get a dependent object error.
alter table TABLE1 alter column COL1 NVarChar(MAX)
so i found this solution here, after you've run the query below, you can alter the column as above.

DECLARE @df SYSNAME
SET @df = 
 (SELECT OBJECT_NAME(cdefault) 
  FROM SYSCOLUMNS 
  WHERE id = OBJECT_ID('dbo.TABLE1') 
   AND name = 'COL1') 
IF @df IS NOT NULL 
 BEGIN 
  EXEC sp_rename @df, 'df_to_drop', 'OBJECT' 
  ALTER TABLE dbo.TABLE1 DROP CONSTRAINT df_to_drop 
 END


Wednesday, 10 September 2008 10:46:01 (GMT Daylight Time, UTC+01:00)  #    Comments [0]  Database

OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview