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: 1254 Visits: 802
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 (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)SSCertifiable (7.7K reputation)

Group: General Forum Members
Points: 7665 Visits: 2776
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 (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14530 Visits: 5177
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: 1254 Visits: 802
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 (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 6975 Visits: 2062
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
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4354 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.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

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

Group: General Forum Members
Points: 1713 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
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25163 Visits: 12598
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
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

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