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.
Related
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.
Q 1:
I have a empty table to insert records. Having one column of IDENTITY type, for which I want to insert values manually.
Example:
Table: Employee
create table Employee
(
ID int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
Inserting records:
SET IDENTITY_INSERT Employee ON;
insert into Employee values(101,'ABC','XYZ','HighStreet','Moscow')
Error:
Msg 8101, Level 16, State 1, Line 1
An explicit value for the identity column in table 'Employee' can only be specified when a column list is used and IDENTITY_INSERT is ON.
Q 2:
How to get latest inserted ID of an Employee without using MAX and Top?
Answer 1
If you will try to insert the value into Identity column you will get the error
Cannot insert explicit value for identity column in table ‘Employee’
when IDENTITY_INSERT is set to OFF.
Write SET IDENTITY_INSERT table name ON before the insert script and
SET IDENTITY_INSERT table name Off after insert script
SET IDENTITY_INSERT Employee ON
insert into Employee(ID,LastName,FirstName,Address,City) values
(101,'ABC','XYZ','HighStreet','Moscow')
SET IDENTITY_INSERT Employee OFF
Answer 2
There are several ways using like this after insert statement
After an INSERT, SELECT INTO, or bulk copy statement is completed,
##IDENTITY contains the last identity value that is generated by the
statement
SELECT ##IDENTITY
It returns the last IDENTITY value produced on a connection,
regardless of the table that produced the value, and regardless of the
scope of the statement that produced the value.
SELECT SCOPE_IDENTITY()
It returns the last IDENTITY value produced on a connection and by a
statement in the same scope, regardless of the table that produced the
value.
SELECT IDENT_CURRENT(‘Employee’)
It returns the last IDENTITY value produced in a table, regardless of
the connection that created the value, and regardless of the scope of
the statement that produced the value. IDENT_CURRENT is not limited by
scope and session; it is limited to a specified table. IDENT_CURRENT
returns the identity value generated for a specific table in any
session and any scope.
MSDN SOURCE
You can use IDENT_CURRENT( 'table_name' ) to get the current value.
For external value of Identity column Column Name given explicitly is Must as below
SET IDENTITY_INSERT Employee ON;
insert into Employee(ID ,LastName,FirstName,Address,City)
values(101,'ABC','XYZ','HighStreet','Moscow')
don't know why this code is not executing...
SET IDENTITY_INSERT COM_MST ON
GO
INSERT INTO COM_MST
SELECT * FROM COM_MST_DEL
and showing an error
An explicit value for the identity column in table 'COM_MST' can only be specified when a column list is used and IDENTITY_INSERT is ON.
You have to specify the columns in the insert statement
SET IDENTITY_INSERT COM_MST ON
GO
INSERT INTO COM_MST
SELECT * FROM COM_MST_DEL
In this your select statement SELECT * FROM COM_MST_DEL might be returning more than the column available in table COM_MST also make sure to specify the column name in Insert statement.
OR
See the solution proposed in the post An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON SQL Server
SET IDENTITY_INSERT table1 ON
This command is giving me error and I need to insert a value into identity column in Postgresql.
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.