Unknown things about Identity Insert:
Considering, am having a requirement to insert the data in the identity column as below,
Code Snippet |
create table VenkatTable (id int identity,[name] varchar(100)) |
insert into VenkatTable values (10,'Santhi') |
You will get an error,
"An explicit value for the identity column in table 'VenkatTable' can only be specified when a column list is used and IDENTITY_INSERT is ON."
Solution:
The problem is with the identity column. It won't allow us to insert the data explicitly inturn we need to switch off the identity property to implement our requirement.
Code Snippet |
set identity_insert VenkatTable on |
insert into VenkatTable values (10,'Santhi') |
On Executing the above statement, am getting the same error.. OOOOPS!!!
"An explicit value for the identity column in table 'VenkatTable' can only be specified when a column list is used and IDENTITY_INSERT is ON."
Let's analyze the problem once again. I've tried with the below statement.
Code Snippet |
insert into VenkatTable(id,[name]) values(10,'Santhi') |
Yahoooo, its inserting the data now. SQL request us to specifically pointout the column names in the insert statement.
After our requirement, we need to Switch off the identity_insert property by using the below statement.
set identity_insert VenkatTable off
Happy Learning!!!
Regards,
Venkatesan prabu .J
No comments:
Post a Comment