Does/can OUTPUT clause turn identity_insert on & off

  • Does/can the OUTPUT clause turn identity_insert on & off ‘behind the scenes’? I think it does, but I can't find that in BOL. Am I correct or missing something?

    Thanks

    Create Table dbo.MyTest (

    Idintidentity(1,1),

    Descriptvarchar(30))

    Insert dbo.MyTest (Descript) values (‘apple’)

    Insert dbo.MyTest (Descript) values (‘banana’)

    Insert dbo.MyTest (Descript) values (‘coffee’)

    Insert dbo.MyTest(Descript) values (‘donut’)

    SELECT TOP (0) * INTO Archive.MyTest FROM dbo.MyTest

    --- the ‘id’ column in Archive.MyTest is an identity column.

    DELETE dbo.myTable

    OUTPUT deleted.id, deleted. Descript INTO Archive.MyTest(id, Descript)

    WHERE id = 3

    /*At this point, should the id value in the Archive table be 1 since that column in archive.MyTest is an identity or should it be the value that was deleted, 3?

    It is a 3, which seems like identity_insert was set on & off. */

    --Then, without explicitly listing the identity column:

    DELETE dbo.myTable

    OUTPUT deleted. Descript INTO Archive.MyTest

    WHERE myTable.Descript = ‘banana’

    --The Id of this row in Archive.MyTest is 4, clearly a new identity value.

  • Missing something, I'm afraid.

    SELECT ... INTO

    always creates a new table. When it creates that table, uses the data types of the source columns, but not primary keys or identity definitions. So, in your example, Archive.MyTest.Id will not be an IDENTITY column, despite your comment to the contrary. It's just a bog-standard INT.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • Phil Parkin (8/17/2012)


    Missing something, I'm afraid.

    SELECT ... INTO

    always creates a new table. When it creates that table, uses the data types of the source columns, but not primary keys or identity definitions. So, in your example, Archive.MyTest.Id will not be an IDENTITY column, despite your comment to the contrary. It's just a bog-standard INT.

    Sorry Phil but this is incorrect - here's the generated script for table Archive.MyTest:

    CREATE TABLE [Archive].[MyTest](

    [Id] [int] IDENTITY(1,1) NOT NULL,

    [Descript] [varchar](30) NULL

    ) ON [PRIMARY]

    Archive.MyTest.Id is an IDENTITY column.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Actually, the BoL states, that you shouldn't include identity columns in the column list of output_table, so reported behaviour is the bug (or free feature, if you like :hehe:).

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • ChrisM@Work (8/17/2012)


    Phil Parkin (8/17/2012)


    Missing something, I'm afraid.

    SELECT ... INTO

    always creates a new table. When it creates that table, uses the data types of the source columns, but not primary keys or identity definitions. So, in your example, Archive.MyTest.Id will not be an IDENTITY column, despite your comment to the contrary. It's just a bog-standard INT.

    Sorry Phil but this is incorrect - here's the generated script for table Archive.MyTest:

    CREATE TABLE [Archive].[MyTest](

    [Id] [int] IDENTITY(1,1) NOT NULL,

    [Descript] [varchar](30) NULL

    ) ON [PRIMARY]

    Archive.MyTest.Id is an IDENTITY column.

    That will teach me to not try and answer questions before drinking coffee, thanks Chris.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • ...

    That will teach me to not try and answer questions before drinking coffee, thanks Chris.

    Seeing your avatar, I wouldn't think that you are great coffee-lover. Are you sure that you need that particular drink, especially on Friday? :hehe::hehe::hehe:

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Eugene Elutin (8/17/2012)


    ...

    That will teach me to not try and answer questions before drinking coffee, thanks Chris.

    Seeing your avatar, I wouldn't think that you are great coffee-lover. Are you sure that you need that particular drink, especially on Friday? :hehe::hehe::hehe:

    Seeing Phil's avatar - like some geezer who's been out on the tiles all night, a whole bucket of the stuff seems entirely appropriate!

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • I think he needs more than coffee - in fact, I think he had more than coffee ... And plenty of the stuFf too.

    But, in the morning, he'll look better than I feel.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

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

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