SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Does/can OUTPUT clause turn identity_insert on & off


Does/can OUTPUT clause turn identity_insert on & off

Author
Message
triloba
triloba
SSC Journeyman
SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)SSC Journeyman (76 reputation)

Group: General Forum Members
Points: 76 Visits: 202
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 (
Id int identity(1,1),
Descript varchar(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.
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51723 Visits: 21170
Missing something, I'm afraid.

SELECT ... INTO [table]

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.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Dave Ballantyne
Dave Ballantyne
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7172 Visits: 8370
Hi, this is a bug

https://connect.microsoft.com/SQLServer/feedback/details/690839/outplut-clause-into-table-with-identity-ignores-set-identity-insert-setting



Clear Sky SQL
My Blog
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41569 Visits: 20005
Phil Parkin (8/17/2012)
Missing something, I'm afraid.

SELECT ... INTO [table]

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
Exploring Recursive CTEs by Example Dwain Camps
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12448 Visits: 5478


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 :heheSmile.

_____________________________________________
"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
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51723 Visits: 21170
ChrisM@Work (8/17/2012)
Phil Parkin (8/17/2012)
Missing something, I'm afraid.

SELECT ... INTO [table]

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.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12448 Visits: 5478

...
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? HeheHeheHehe

_____________________________________________
"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
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41569 Visits: 20005
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? HeheHeheHehe


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
Exploring Recursive CTEs by Example Dwain Camps
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51723 Visits: 21170
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.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search