Trying to use a parameter creating an identity column.

  • Hi:

    I'm trying to do this:

    declare @maxid as int

    set @maxid = (select maxid from MAXIDCUSTOMER) + 1

    alter table att_list_2

    add recid [int] IDENTITY(@maxid,1) NOT NULL

    but it gives me an error:

    "Incorrect syntax near '@maxid'."

    Is it possible to use a parameter on the identity?

  • I don't think it's possible to use variables in DDL statements. You have use a dynamic SQL statement.

  • I just solved it:

    declare @maxid as int

    set @maxid = (select maxid from MAXIDCUSTOMER) + 1

    declare @query as varchar(200)

    set @query = 'alter table att_list_2 add recid [int] IDENTITY(' + cast(@maxid as varchar(10)) + ',1) NOT NULL'

    exec (@query)

    Thanks for the help anyway!

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

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