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


FK to IDENTITY


FK to IDENTITY

Author
Message
nenad-zivkovic
nenad-zivkovic
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1185 Visits: 786
Comments posted to this topic are about the item FK to IDENTITY

_______________________________________________
www.sql-kefalo.net (SQL Server saveti, ideje, fazoni i fore)
kapil_kk
kapil_kk
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5346 Visits: 2767
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/
Igor Micev
Igor Micev
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10372 Visits: 5157
Hi,
Nice question! Bravo.

Thank you
IgorMi

Igor Micev,
My blog: www.igormicev.com
nenad-zivkovic
nenad-zivkovic
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1185 Visits: 786
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)
call.copse
call.copse
SSCertifiable
SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)

Group: General Forum Members
Points: 5638 Visits: 2009
I thought the question was expressed well Nenad and had no problem with the clarification provided by the specified identity insert value - cheers.
Raghavendra Mudugal
Raghavendra Mudugal
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3370 Visits: 2958
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.
Dineshbabu
Dineshbabu
UDP Broadcaster
UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)

Group: General Forum Members
Points: 1436 Visits: 569
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..
sqldoubleg
sqldoubleg
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1461 Visits: 1363
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
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18877 Visits: 12426
Very good, and very clever question. Well done! Wink


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Thomas Abraham
Thomas Abraham
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3839 Visits: 2256
I too found the question clear. Excellent question, and well posed.

Please don't go. The drones need you. They look up to you.
Connect to me on LinkedIn
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