Error creating entity Microsoft SQL server IDENTITY_INSERT - sql-server

I'm using JHipster 4. I've create a simple entity just with a "name" property and when I try to create a entity from UI I get the folowing error. (I'm using Microsoft SQL Server).
In think the important part of the error is :
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'tenant' when IDENTITY_INSERT is set to OFF.

Direct answer appears to be that you just need to configure jhipster to have identity_insert on, per this link: https://jhipster.github.io/tips/004_tip_using_ms_sql_server.html
Adding the identityInsertEnabled="true" is the same as wrapping your Inserts with IDENTITY_INSERT ON and IDENTITY_INSERT OFF which will allow you to insert the project autogenerated identities directly. This is why we are using the MS SQL Server Liquibase for.
The error tells you everything you need to know. You are trying to insert a value in to an identity column. This is not allowed, as the column is supposed to be automatically populated unless you explicitly turn it off temporarily.
Documentation on what identity_insert is here: https://msdn.microsoft.com/en-us/library/ms188059.aspx
You can turn it off using the command set identity_insert SchemaName.TableName off though you had better be very confident you are inserting the correct data.
I would recommend you do some investigation with whoever manages your database as to why that column is an identity column and whether or not you should be inserting into it at all.
Assume a table:
create table test(ID int identity(1,1)
,Name nvarchar(100)
)
ALL of these insert statements will throw an error:
insert into test(ID,Name)
select ID
,Name
from OtherTable
insert into test(ID,Name)
select null as ID
,'TestName' as Name
insert into test(ID,Name)
values(99
,'Name'
)
Whereas these will work, automatically generating a new, unique value for the ID column:
insert into test(Name)
select Name
from OtherTable
insert into test(Name)
select 'TestName' as Name
insert into test(Name)
values('Name')
In short, you need to insert nothing into the identity column. Not a blank string, not a null value, but absolutely nothing at all.

Related

Is there an equivalent of rowversion in SQL Server, but for a table?

I am looking for something similar to rowversion but for the whole table : a unique number automatically generated that can be used to know if table has changed.
Eg :
SELECT TABLE_VERSION('FOO')
0x00000000000007D2
INSERT INTO FOO (...) VALUES (1, 'Hello world')
SELECT TABLE_VERSION('FOO')
0x00000000000007D5
Does it exists in SQL Server ?
If not, is there any alternative ? Somethings that allow to track changes on a column would be great too.
I have been thinking of using HASHBYTES, but unfortunately it only gives hash for a single column value not for the whole column.
There is not such a thing in SQL Server, however you can implement it easly with triggers. I have used it to assist cache invalidation for web apps.
first create the table etags:
CREATE TABLE etags
(
etag_name varchar(128) NOT NULL PRIMARY KEY,
etag_value uniqueidentifier NOT NULL
)
Insert a value for the Foo table:
INSERT INTO etags VALUES ('foo', NEWID())
Then create a trigger for the foo table that updates the etag value when table changes
CREATE TRIGGER foo_etag
ON foo FOR INSERT, UPDATE, DELETE
AS
SET NOCOUNT ON
UPDATE etags SET etag_value = NEWID()
WHERE etag_name = 'Foo'

SQL Server : insert into fails because of entity insert is off

I can't figure out what I am doing wrong.
This is my code
SET IDENTITY_INSERT [Erea_Local].[Domain].[BlikAfmeting] ON
INSERT INTO [Erea_Local].[Domain].[BlikAfmeting]
SELECT *
FROM [dbo].[BlikAfmetings]
SET IDENTITY_INSERT [Erea_Local].[Domain].[BlikAfmeting] OFF
This is the error
An explicit value for the identity column in table 'Erea_Local.Domain.BlikAfmeting' can only be specified when a column list is used and IDENTITY_INSERT is ON.
If I only run the set IDENTITY INSERT .....
I get the message command completed
According the message:
An explicit value for the identity column in table
'Erea_Local.Domain.BlikAfmeting' can only be specified when a column
list is used and IDENTITY_INSERT is ON.
You should use a column list of values.

error IDENTITY_INSERT is set to OFF but actually it is ON

I try to insert data to MSSQL DB through Entity Framework to table. It throws error "Cannot insert explicit value for identity column in table 'Product' when IDENTITY_INSERT is set to OFF." but my ID have identity set to on. Anyone run into this trouble before? Of course i do not inserting value into Identity column
funny thing happened , i changed identity on database table but forgot to update ADO.NET model...

Getting the primary key of an newly inserted row in SQL Server 2008

I have a bunch of data which will insert into a table. This issue is that I need it to return the primary key to that table. I wasn't sure if there was things like:
insert into TABLE (...) values (...) RETURNING p_key
or
select p_key from (insert into TABLE (...) values (...))
I am making a workaround for a browser and saved information which will more or less add a row and then update it... but without the primary key, there is no way to update it as there is no reference to it.
I was looking online and found some examples via google, but it confused me slightly with these examples.
http://en.wikipedia.org/wiki/Insert_(SQL)#Retrieving_the_key
http://www.daniweb.com/web-development/databases/ms-sql/threads/299356/returning-identity-of-last-inserted-row-uniqueidentifier
Wikipedia was saying that for SQL Server 2008 to use OUTPUT instead of RETURNING, possible to use something like OUTPUT p_key
If you're inserting a whole set of rows, selecting the SCOPE_IDENTITY() won't do. And SCOPE_IDENTITY also only works for (numeric) identity columns - sometimes your PK is something else...
But SQL Server does have the OUTPUT clause - and it's very well documented on MSDN!
INSERT INTO dbo.Table(columns)
OUTPUT INSERTED.p_key, INSERTED.someothercolumnhere .......
VALUES(...)
Those values will be "echoed" back to the calling app, e.g. you'll see them in a grid in SQL Server Management Studio, or you can read them as a result set from your C# or VB.NET calling this INSERT statement.
Scope_Identity() is what you want, assuming that by "primary key" you mean "Identity"
declare #id int
insert yourtable values (some, values)
select #id = Scope_Identity()
In C#, right after your SQL Statement write SELECT SCOPE_IDENTITY(); so your code would be:
insert into TABLE (...) values (...); SELECT SCOPE_IDENTITY();
then, instead of executeNonQuery use executeScalar.
That should do the trick!
After performing insert, query:
select scope_identity()
to retrieve last inserted primary key.
Using ##IDENTITY , you can get the last generated primary key
Insert into TableName (Name,Class) values('ABC','pqr')
select ##IDENTITY

Help needed with "Cannot insert explicit value for identity column" Error

I have a table Table1 on which I have created an insert/update trigger. This trigger insert records of inserts/updates on Table1 into Table1_history. To test whether the trigger is inserting records into Table1_history, I have inserted some test data into Table1 and I am receiving the error:
Cannot insert explicit value for identity column in table 'Table1_history' when IDENTITY_INSERT is set to OFF
Schema of Table1 and Table1_history are identical except an additional 'inserted_on_date' field in Table1_history.
I have set the Identity Insert to ON and the trigger works. But do I have to do this every time I need to insert a new record in Table1?
Put the IDENTITY INSERT ON into the trigger itself, it only needs to be active while you are pushing values into table1_history, because that is where the problem is.
Or, in Table1_history, don't make the pk field an IDENTITY, it does not need to be because you are supplying the values from Table1.
I assume your 'inserted_on_date' field is a datetime. You should be able to turn off the identity insert and set the default value for this column to GETDATE(), which will set any newly inserted records to the datetime the server has when the record is inserted.
You don't mention what database but I assume it's SQL Server 2008.
The set identity on command is session specific. So yes, you need to issue it each time.
Instead, you should remove the identity specification from the history table. You don't need it.

Resources