April 23, 2013 at 10:55 pm
Comments posted to this topic are about the item FK to IDENTITY
_______________________________________________
www.sql-kefalo.net (SQL Server saveti, ideje, fazoni i fore)
April 23, 2013 at 11:29 pm
Hi,
I was not aware of the fact that after making
SET IDENTITY_INSERT dbo.Test OFF;
identity value gets incremented. I am confused now as what is the purpose of making Identity_Insert OFF then?
Lets check this scenario:
--Scenario 1
TRUNCATE TABLE dbo.test --- All data gets removed now.
SELECT IDENT_CURRENT('test') -- return 1
SET IDENTITY_INSERT dbo.Test OFF;
INSERT INTO dbo.Test (Col_2) VALUES (1); -- 1 rows get affected
INSERT INTO dbo.Test (Col_2) VALUES (2); -- 1 rows get affected
INSERT INTO dbo.Test (Col_2) VALUES (3); -- 1 rows get affected
Select * from dbo.test (Now it will return 3 rows)
--Scenario 2
TRUNCATE TABLE dbo.test --- All data gets removed now.
SELECT IDENT_CURRENT('test') -- return 1
SET IDENTITY_INSERT dbo.Test OFF;
INSERT INTO dbo.Test (Col_2) VALUES (2); -- Error return
INSERT INTO dbo.Test (Col_2) VALUES (2); -- 1 rows get affected
INSERT INTO dbo.Test (Col_2) VALUES (3); -- 1 rows get affected
Select * from dbo.test (Now it will return 2 rows)
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
April 24, 2013 at 12:48 am
Hi,
Nice question! Bravo.
Thank you
IgorMi
Igor Micev,My blog: www.igormicev.com
April 24, 2013 at 1:38 am
kapil_kk (4/23/2013)
Hi,I was not aware of the fact that after making
SET IDENTITY_INSERT dbo.Test OFF;
identity value gets incremented. I am confused now as what is the purpose of making Identity_Insert OFF then?
When IDENTITY_INSERT is set to OFF, values for identity columns gets incremented automatically, that is a default setting. That line makes no difference in the query - it's only purpose was to make sure it's set to OFF and remove possible confusion whenever IDENTITY_INSERT was ON or OFF. If IDENTITY_INSERT was set to ON, all inserts would fail for not providing value for Col_1.
I apologize If it brought additional confusion instead. It was not meant in that way.
Now, that I think of it, it is kind of a additional test to see if people know what ON and OFF values for IDENTITY_INSERT mean, which is not that bad.
Best Regards
-Nenad
_______________________________________________
www.sql-kefalo.net (SQL Server saveti, ideje, fazoni i fore)
April 24, 2013 at 2:20 am
I thought the question was expressed well Nenad and had no problem with the clarification provided by the specified identity insert value - cheers.
April 24, 2013 at 3:10 am
Very interesting... thank you for the post. 🙂
ww; Raghu
--
The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.
April 24, 2013 at 4:20 am
nenad-zivkovic (4/24/2013)
kapil_kk (4/23/2013)
Hi,I was not aware of the fact that after making
SET IDENTITY_INSERT dbo.Test OFF;
identity value gets incremented. I am confused now as what is the purpose of making Identity_Insert OFF then?
When IDENTITY_INSERT is set to OFF, values for identity columns gets incremented automatically, that is a default setting. That line makes no difference in the query - it's only purpose was to make sure it's set to OFF and remove possible confusion whenever IDENTITY_INSERT was ON or OFF. If IDENTITY_INSERT was set to ON, all inserts would fail for not providing value for Col_1.
I apologize If it brought additional confusion instead. It was not meant in that way.
Now, that I think of it, it is kind of a additional test to see if people now what ON and OFF values for IDENTITY_INSERT mean, which is not that bad.
Best Regards
-Nenad
No confusion at all, Very clear. Good work Nenad.
But i don't think it's a common practice to ensure every time whether IDENTITY_INSERT is set to OFF, because it is the default behavior SQL server. Only when we need to insert explicit values to identity column we will use this SET option.
--
Dineshbabu
Desire to learn new things..
April 24, 2013 at 4:56 am
Really good question, tricky and nice.
Obviously the default value for IDENTITY_INSERT is OFF, only by changing it to ON, you wouldn't be able to insert any of the rows as values for column 1 should be provided. (That was the tricky part, setting to OFF does not do any)
Also nice to note that the failed INSERT statements would affect the identity value for the table, though the row didn't end up there.
Thanks
April 24, 2013 at 5:04 am
Very good, and very clever question. Well done! 😉
April 24, 2013 at 5:36 am
Great QOTD today! Reminded that failed inserts still increase the identity. And, if you did NOT include the SET IDENTITY OFF statement, then people would have been complaining that you tried to trick them.
April 24, 2013 at 5:58 am
Interesting, thanks it was a good question.
April 24, 2013 at 6:22 am
Great question! Thank you for keeping the code clear and focused.
April 24, 2013 at 6:31 am
It would have been interesting to include exactly which value(s) would be inserted. If you weren't aware that the identity would be consumed, you might believe that the value 1 would successfully insert - which would give you the same, correct answer.
ron
-----
a haiku...
NULL is not zero
NULL is not an empty string
NULL is the unknown
April 24, 2013 at 6:56 am
nenad-zivkovic (4/24/2013)
kapil_kk (4/23/2013)
Hi,I was not aware of the fact that after making
SET IDENTITY_INSERT dbo.Test OFF;
identity value gets incremented. I am confused now as what is the purpose of making Identity_Insert OFF then?
When IDENTITY_INSERT is set to OFF, values for identity columns gets incremented automatically, that is a default setting. That line makes no difference in the query - it's only purpose was to make sure it's set to OFF and remove possible confusion whenever IDENTITY_INSERT was ON or OFF. If IDENTITY_INSERT was set to ON, all inserts would fail for not providing value for Col_1.
I apologize If it brought additional confusion instead. It was not meant in that way.
Now, that I think of it, it is kind of a additional test to see if people know what ON and OFF values for IDENTITY_INSERT mean, which is not that bad.
Best Regards
-Nenad
Hi,
Thanks for your explanation...:-)
I was not confused with your explanation instead I was thinking why this is happening that even after SET identity_insert OFF still values are getting incremented :w00t:
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Viewing 15 posts - 1 through 15 (of 35 total)
You must be logged in to reply to this topic. Login to reply