Programatically set default to an existing db column

  • Thanks for your help. (mssqlserver 2005) I have a table called tier1 and a column named class nvarchar(150) it needs to have a default value set on that column. I know how to do it using managment studio, but I need to do it using code. How would I do this? Thanks!

  • Something like this.

    ALTER TABLE tblA

    ADD Class NVARCHAR(150) DEFAULT 'Some value'

  • George Murphy (4/30/2010)


    Thanks for your help. (mssqlserver 2005) I have a table called tier1 and a column named class nvarchar(150) it needs to have a default value set on that column. I know how to do it using managment studio, but I need to do it using code. How would I do this? Thanks!

    George just before saving a change like adding this default in the Design GUI, you could click the "Generate Change Script " button(the first button...scroll bar and save kind of icon) and see the code that the system would use;

    it's just adding a default constraint, which would be like this:

    ALTER TABLE tier1 ADD CONSTRAINT [DF_MyDefaultForClass] DEFAULT 'TheDefaultClass' FOR class

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply