lundi 2 novembre 2015

Insert new row with current Identity as value

Don't ask me why, but I want to have a column where Identity is stored as varchar. Is it possible to assign this during creation or do I need to Scope Identity and Update?

Normally I would do this:

INSERT INTO [User]
(
    -- id -- this column value is auto-generated,
    useridcr,
    dtcr,
    varcharid
)
VALUES
(
    25,
    '2015-01-30 00:00:00.000'
    ''
)

SET @id = SCOPE_IDENTITY()

UPDATE [User]
SET
    varcharid = @id
WHERE id = @id

But is it possible to know the value of new identity before it's created? So I could use the value in the insert statement.

Thank you

Aucun commentaire:

Enregistrer un commentaire