When I run in java netbeans:
executeUpdate("INSERT INTO TableName (id, name) VALUES (1, 'Name1')")
I get the error:
Cannot insert explicit value for identity column in table 'TableName' when IDENTITY_INSERT is set to OFF
If I run:
executeUpdate("SET IDENTITY_INSERT TableName ON;INSERT INTO TableName (id,name) VALUES (1,'Name1');SET IDENTITY_INSERT TableName OFF;")
I get this error:
Cannot find the object "TableName" because it does not exist or you do not have permissions.
Why does this happen and how can I solve this?
It is sufficient to give ALTER rights.
GRANT ALTER TO USER
https://sqlblog.org/2010/12/14/what-permissions-are-required-for-set-identity_insert-on
Just let the IDENTITY property do what it is supposed to an only pass in the name. No need explicitly attempt to pass in an ID unless you are trying to associate specific ID's with names, in which you'd have to keep up with the values you have used and haven't used, and IDENTITY would then be sort of useless. I'd just add a unique constraint on the ID column in this case.
INSERT INTO TableName (name) VALUES ('Name1')
Related
i have two table userGold and AspNetUserRoles(UserId,RoleId) .
primary key type of UserGold is nvarchar(450)
same for AspNetUserRole.
my problem is that i want to get the last inserted primary key in UsereGold and insert it in AspNetUserRoles table using a trigger.
SCOPE_IDENTITY didn't work cause my primary key type is nvarchar.
i don't know what to do.
i saw solution like output inserted but it didn't work
create trigger addrole
on UserGold
after Insert
as
Begin
declare
#userid nvarchar(450)
set #userid=CAST(SCOPE_IDENTITY() AS nvarchar(450))
insert into AspNetUserRoles(UserId,RoleId)
values(#userid,'2c258e8d-c648-4b38-9b01-989d4dd525fe')
end
You should not write triggers - or anything else in a database - that only expect a single row to change. Databases work with sets of information, not single "records". Imagine what happens if someone create 5 rows in UserGold in a single insert statement. You can't put 5 userid values into a single #userId variable.
What you want is something like
-- assuming your tables are in the dbo schema! Make sure you include the schema name
create trigger AddDefaultRole on dbo.UserGold after insert as begin
set nocount on;
insert dbo.AspNetUserRoles (UserId, RoleId)
select UserId, '2c258e8d-c648-4b38-9b01-989d4dd525fe'
from inserted;
end
For more information, see Inserted and Deleted tables for triggers
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.
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.
I need to transfer a table from database A to database B, the table has an autoincrement column. The content of the table in the new database has to be identical to the table in the original database, including the values in the autoincrement column.
The following LOAD TABLE, where col1 is the autoincrement column, doesn't work:
set identity_insert TableName on;
LOAD TABLE TableName
(col1,col2,col3)
FROM 'file.csv';
I get the following error:
Cannot insert or update Column col1: set option 'identity_insert' to the specific table name containing the identity column to be modified.
Still, I'm setting the identity_insert option. What's wrong with this command?
Please try
SET TEMPORARY OPTION IDENTITY_INSERT = 'MyTable';
This has been discussed on SCN
DEFAULTS OFF looks like it should be used as an option if you want to ovverride the default identity (autoincrement) value.
see: http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc00801.1530/doc/html/san1281564935243.html
If that doesn't work, and this is just a one time thing... the simplest way to accomplish this would be to load table into TableName_work that has no autoincrement, then insert the data into your destination via a select statement with identity_insert on.
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