SQL Server Performance

setting default value for a column

Discussion in 'General Developer Questions' started by suresh_sajja, Jan 12, 2005.

  1. suresh_sajja New Member

    i am using sql server 2000. In one table, i set the defalut value for a column to 0. it worked fine. but after one week, the column have null value instead of zero. i didn't understad what's the problem
  2. amitm79 New Member

    Hi Suresh,
    Default value will only be put if you don't include that column in the list while inserting. If you include this column in the insert list and specify a value NULL for it, it will take it.


    Regards,
    Amit
  3. Adriaan New Member

    The column obviously allows nulls, and someone has been inserting rows with a null value for that column.

    A default value is inserted only when the insert action does not "mention" the column.

    CREATE TABLE T1 (col1 INT NULL, col2 VARCHAR(10) DEFAULT 'X' NULL)
    INSERT INTO T1 (col1) VALUES (1)
    INSERT INTO T1 (col1, col2) VALUES (2, NULL)
    INSERT INTO T1 (col1, col2) VALUES (3, 'Jantje')
    Results:
    col1col2
    1X
    2
    3Jantje

Share This Page