Results 1 to 7 of 7

Thread: [RESOLVED] Removing the Deafult Value from a SQL Server 2005 Column

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Resolved [RESOLVED] Removing the Deafult Value from a SQL Server 2005 Column

    No-one here at work has ever had to perform this specific task before so no-one can tell me how they did it. I've read the documentation for the ALTER TABLE statement in Books Online and the syntax starts like this:
    Code:
    ALTER TABLE table_name
    { [ ALTER COLUMN column_name 
       {DROP DEFAULT 
       | SET DEFAULT constant_expression 
       | IDENTITY [ ( seed , increment ) ]
       }
    Seems simple enough but when I try to execute this:
    SQL Code:
    1. ALTER TABLE TruckRun
    2.     ALTER COLUMN WinUserName DROP DEFAULT
    in Management Studio I get this:
    Msg 156, Level 15, State 1, Line 2
    Incorrect syntax near the keyword 'DEFAULT'.
    I must be missing something elementary but that looks like it should work to me.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Removing the Deafult Value from a SQL Server 2005 Column

    Hehe. It would help if I looked at the T-SQL reference rather than the CE reference. In my defense I did filter by SQL Server 2005 rather than SQL Server 2005 Compact Edition, but the index seems to include both in that case, with CE being the first.

    Anyway, even having looked at the correct documentation I still can't see how to do it. I originally added the column like this:
    SQL Code:
    1. ALTER TABLE TruckRun
    2.     ADD WinUserName varchar(100) NOT NULL DEFAULT ''
    the default value was required because I'm adding a NOT NULL column to a table that already contains data. I then populate the column with data like so:
    SQL Code:
    1. UPDATE TruckRun
    2. SET WinUserName =
    3. (
    4.     SELECT WinUserName
    5.     FROM User
    6.     WHERE UserID = TruckRun.UserID
    7. )
    and now I want to remove the default value. I tried this:
    SQL Code:
    1. ALTER TABLE TruckRun
    2.     ALTER COLUMN WinUserName varchar(100) NOT NULL
    which runs OK but doesn't remove the default. I also tried this:
    SQL Code:
    1. ALTER TABLE TruckRun
    2.     ALTER COLUMN WinUserName varchar(100) NOT NULL DEFAULT NULL
    which, not unexpectedly, gives me this:
    Msg 156, Level 15, State 1, Line 2
    Incorrect syntax near the keyword 'DEFAULT'.
    I'll keep looking but if anyone has an idea in the mean time it would be appreciated.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Removing the Deafult Value from a SQL Server 2005 Column

    Did you try Alter Table Drop Constraint (the constraint being the Default)?

    SQL Code:
    1. ALTER TABLE TruckRun
    2.      Drop Constraint [i]Constraint Name Goes Here[/i]

    I found this link also

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=104804
    Last edited by GaryMazzone; Aug 11th, 2008 at 07:22 AM.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Removing the Deafult Value from a SQL Server 2005 Column

    Quote Originally Posted by GaryMazzone
    Did you try Alter Table Drop Constraint (the constraint being the Default)?

    SQL Code:
    1. ALTER TABLE TruckRun
    2.      Drop Constraint [i]Constraint Name Goes Here[/i]

    I found this link also

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=104804
    I didn't but I will. I'm at home at the moment but I'll give it a try when I get to work in the morning. Thanks for the input.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Removing the Deafult Value from a SQL Server 2005 Column

    Thanks Gary. That was exactly what I needed. Getting the constraint name was a little bit of fun and games. Here's what I ended up with:
    SQL Code:
    1. DECLARE @ParentObjectID int,
    2.         @ParentColumnID int,
    3.         @ConstraintName nvarchar(100),
    4.         @SqlCmd nvarchar(100)
    5.  
    6. SET @ParentObjectID = OBJECT_ID(N'TruckRun')
    7.  
    8. SELECT @ParentColumnID = column_id
    9. FROM sys.columns
    10. WHERE object_id = @ParentObjectID
    11.     AND name = N'WinUserName'
    12.  
    13. SELECT @ConstraintName = name
    14. FROM sys.default_constraints
    15. WHERE parent_object_id = @ParentObjectID
    16.     AND parent_column_id = @ParentColumnID
    17.  
    18. -- The constraint name must be specified literally,
    19. -- but it can only be determined by querying the sys.default_constraints view.
    20. SET @SqlCmd = N'ALTER TABLE TruckRun DROP CONSTRAINT ' + @ConstraintName
    21. EXEC sp_executesql @SqlCmd
    Last edited by jmcilhinney; Aug 14th, 2008 at 01:41 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [RESOLVED] Removing the Deafult Value from a SQL Server 2005 Column

    Glad to finally be able to help you out.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Removing the Deafult Value from a SQL Server 2005 Column

    I had forgotten that this application was going to deployed against SQL Server 2000 so, for completeness, here's the SQL code that works on that version:
    SQL Code:
    1. DECLARE @TableID int,
    2.         @ColumnID int,
    3.         @ConstraintID int,
    4.         @ConstraintName nvarchar(100),
    5.         @SqlCmd nvarchar(100)
    6.  
    7. SET @TableID = OBJECT_ID(N'TruckRun')
    8.  
    9. SELECT @ColumnID = colid
    10. FROM syscolumns
    11. WHERE   id = @TableID
    12.     AND name = N'WinUserName'
    13.  
    14. SELECT @ConstraintID = constid
    15. FROM sysconstraints
    16. WHERE   id = @TableID
    17.     AND colid = @ColumnID
    18.  
    19. SELECT @ConstraintName = name
    20. FROM sysobjects
    21. WHERE id = @ConstraintID
    22.  
    23. -- The constraint name must be specified literally,
    24. -- but it can only be determined by querying the sysobjects table.
    25. SET @SqlCmd = N'ALTER TABLE TruckRun DROP CONSTRAINT ' + @ConstraintName
    26. EXEC sp_executesql @SqlCmd
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width