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.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1305 Visits: 803
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
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: 10619 Visits: 2778
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
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19718 Visits: 5191
Hi,
Nice question! Bravo.

Thank you
IgorMi

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

Group: General Forum Members
Points: 1305 Visits: 803
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
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8919 Visits: 2157
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
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5536 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
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1908 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
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2105 Visits: 1377
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
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

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


Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Thomas Abraham
Thomas Abraham
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 6033 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