SQL Default Values

  • I need to make a column have a value of 4 zero's plus a column that is already in the database.

    Example

    ACCT = 001

    I need acct1 to be = 0000001

    I would like this to happen every time a row is inserted.

    Thanks for any help

    Alexzander N. Nepomnjashiy

    Technical editor for Wrox Press:
    "SQL Server 2000 Fast Answers"

    Technical editor for Apress:
    "Honeypot for Windows"
    "SQL Server Yukon Revealed"

  • You should use a trigger for this.

    Default values only accept constants.

  • Thank you

    Alexzander N. Nepomnjashiy

    Technical editor for Wrox Press:
    "SQL Server 2000 Fast Answers"

    Technical editor for Apress:
    "Honeypot for Windows"
    "SQL Server Yukon Revealed"

  • If column ACCT1 needs to change when ACCT changes, consider concatenating the 4 zeros in your application instead of storing it in a table. (after fetching the rows).

    Saves Disk space and Network I/O on the server.

  • Try also a computed column.  Eg.

    create table xxx

    (acct char(3),

    acct1 as '0000' + acct)

    insert xxx values ('001')

    insert xxx values ('002')

    insert xxx values ('003')

    select * from xxx

    acct acct1  

    ---- -------

    001  0000001

    002  0000002

    003  0000003

    (3 row(s) affected)

     

    update xxx set acct = replace(acct, '0', '1')

    select * from xxx

    acct acct1  

    ---- -------

    111  0000111

    112  0000112

    113  0000113

    (3 row(s) affected)

     

     


    Cheers,
    - Mark

  • Mark, thank you VERY MUCH!

    Alexzander N. Nepomnjashiy

    Technical editor for Wrox Press:
    "SQL Server 2000 Fast Answers"

    Technical editor for Apress:
    "Honeypot for Windows"
    "SQL Server Yukon Revealed"

  • OK, now alexnep knows how to do it, but SeekQuel had the correct answer albeit for the wrong reasons.  Adding the column by whatever means is still a clear violation of 1NF and should never be done.

  • I knew I'm a natural talent without knowing it !

Viewing 8 posts - 1 through 7 (of 7 total)

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