-1

I am new to SQL Server. It may be the very basic question.

I want to set a default value for a column having null values and which is already defined in SQL Server 2012.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sony
  • 3
  • 5
  • Not clear. You can put a default on a column, and if the column isn't specified in the insert it will be used. If you specify it but use null as a value, then null it will be, but if you are putting a null in there anyway, you could put a default in... – Tony Hopkinson Mar 24 '15 at 17:31
  • @TonyHopkinson I think he is trying to create a default value for a column in an existing table. Is this correct @Sony? – Aidan Mar 24 '15 at 17:43
  • If the column already exists, adding a default constraint to it will **not** touch the existing values (even if they are `NULL`). If you *add* a new column - then yes, you can set a default value and have it applied to all rows. For an existing column, you need to set the default constraint and then separately, in a second step, update the existing values – marc_s Mar 24 '15 at 17:46
  • I have the column which is currently having null values... I want to put default values instead of null – sony Mar 24 '15 at 18:09
  • @Aidan, good guess apparently. – Tony Hopkinson Mar 24 '15 at 22:01

1 Answers1

0

I think what you are trying to do is answered in this question.

ALTER TABLE TableName ADD CONSTRAINT ConstraintName DEFAULT N'DefaultValue' FOR ColumnName;
Aidan
  • 171
  • 3
  • 12