I am doing an InserAsync and the SQL query returns the generated Id but the entity has zero for its integer identity column.
Why is it not updating the identity column? It works when it's Insert but not when it's InsertAsync. Such is life...
I used Insert without Async and returned Task.FromResult(0); from the method.
Boom, that will learn 'em.
Related
The query to create the table with its respective field set as IDENTITY is the following:
CREATE TABLE user (
email varchar(100) primary key
name varchar(30),
pwd varchar(10)
)
Alter table to add IDENTITY field:
ALTER TABLE user ADD id int /*NOT NULL*/ IDENTITY;
The email field to be PRIMARY KEY INDEX will fail if a NULL or DUPLICATED value was set, for example supposed that myemail#domain.com already exists, OK the query fails, but I change the email to anotheremail#domain.com SQL Server generate a new one value for the IDENTITY field based on the query(s) that failed before. My question is why does this happen? (Is this ONLY on SQL Server or other database providers also)
Well, this is clearly documented in "CREATE TABLE (Transact-SQL) IDENTITY (Property)":
Reuse of values - For a given identity property with specific seed/increment, the identity values are not reused by the engine. If a particular insert statement fails or if the insert statement is rolled back then the consumed identity values are lost and will not be generated again. This can result in gaps when the subsequent identity values are generated.
Further along the documentation also answers why and suggests what to do if this is not acceptable:
These restrictions are part of the design in order to improve performance, and because they are acceptable in many common situations. If you cannot use identity values because of these restrictions, create a separate table holding a current value and manage access to the table and number assignment with your application.
I have linked a basic database containing 2 tables, Login and Customer:
Login contains (CustomerID, loginID, password)
Customer contains (CustomerID, name, last, dob)
CustomerID is primary key in both fields, and is the auto-increment value in the Login table.
When I want to register a new user, I want to FIRST populate Login table (which has CustomerID as an auto-increment value)
How do I get the next auto-increment value from Login so I could use it when populating the Customer table?
I am using SQL Server 12.
If you are using a Stored Procedure to handle this logic, then you can do this in one transaction by getting the Scope_Identity value from the newly inserted record, and then using this value to insert into the next table.
Scope_Identity is definitely the way to go here, and you will get the latest ID within the scope of your call to the SP.
If this is a two step process via Entity Framework, then take a look at How can I retrieve Id of inserted entity using Entity framework?
select isnull(MAX(CustomerID),0)+1 from Customer
If you are using sql queries:
let your inertIntoLoginTable function return an integer which is the SCOPE_IDENTITY
or ##Identity
If you are using an ORM like Entity Framework, the id will be set to the object after insertion in the database:
Login.loginID will be the last id inserted after you call dbContext.SaveChanges();
Try this SQL statement:
SHOW TABLE STATUS LIKE 'Login_Table'
Seek the value of Auto_increment or other details
You could use a Guid in your C# code and set that as the CustomerID, (Guid custID = new Guid();).
Guids are the type uniqueidentifier in SQL Server.
Are you using Entity Framework? It's a great way to access a SQL database from a .NET application.
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.
using replication in sql srv causes the addition of this guid field, it also adds a value to it
but when i insert new records to the db, i have to give somthing or the guid field
it should be like aaaaa-aaa-something and unique!!
this is a problem for me , how am i supposed to do this keeping it unique every time?
should sql srv automatically add some yhing?
The ROWGUIDCOL added by Merge Replication should be populated with... guids:
ROWGUIDCOL does not enforce uniqueness
of the values that are stored in the
column and does not automatically
generate values for new rows that are
inserted into the table. To generate
unique values for each column, either
use the NEWID function on INSERT
statements or specify the NEWID
function as the default for the
column.
I want to insert an item to a table in the database. The table has a key that is auto generated. Is it possible to override the auto generated key, (force a value). If so how?
I'm going to assume you are talking about identity columns and not sequences.
In DB2's CREATE TABLE syntax, have a look at the "generated-column-spec" in the syntax diagram as it relates to identity columns. There are two ways to specify how the identity value will be generated:
GENERATED ALWAYS: this option will always generate and identity value, and you cannot specify a value for the identity column in an insert statement
GENERATED BY DEFAULT: this option will generate an identity value if no value for the column is specified in the insert statement. If you provide a value for the column in the insert statement, db2 will not generate an identity value for it.
If the table you are trying to insert into used the ALWAYS option when the table is created, then you cannot override it. You'll need to drop and recreate the table or use the ALTER TABLE statement to redefine the column to generate the identity value by default only.
If you're trying to LOAD data into a table that has an identity column which is GENERATED ALWAYS then you can do this:
db2 load from tab43.ixf of ixf modified by identityoverride into tablename