Sequence as default value for a column

sequence, sql-server, sql-server-2012, t-sql

Solution

It turned out to be easy enough:

create table mytable (
    id      bigint not null constraint DF_mytblid default next value for mainseq,
    code    varchar(20) not null
)

or if the table is already created:

alter table mytable
add constraint DF_mytblid
default next value for mainseq
for id

(thank you Matt Strom for the correction!)

Problem

I have already created a sequence: ``` create sequence mainseq as bigint start with 1 increment by 1 ``` How do I use this sequence as the default value of a column? ``` create table mytable( id bigint not null default mainseq -- how? code varchar(20) not null ) ```

Original source