Summary on 20080701: insert value for identity column in sql
2008-07-02 21:39
441 查看
In MSSQLServer, by default, user can‘t insert/assign/update value for identity column; SQLServer will do this for user automatically, so there is one problem caused that: what will do if wants to assign value for identity column? for one table like: teacher(teacher_id, teacher_name, teacher_age), and teacher_id is identity the insert should be like: insert into teacher values('kalash',12), that's correct format, if insert is like: insert into teacher values(1,'kalash',12), there will be error when execute the statement
and for the case that user want to insert value for identity column, user can turn on the identity_insert for implicitly insert, the code is as followed:
for the above code, please note that: 1. for the insert, the column name should be listed out, especially the 'teacher_id' 2. any time, the 'identity_insert' can be set to 'on' for only one table, if the 'identity_insert' is set to 'on' and set that for another table, there will be error, so for tip3 3. it's suggested that every time after set the identity_insert to be 'on', set it back to 'off' after the operation
and for the case that user want to insert value for identity column, user can turn on the identity_insert for implicitly insert, the code is as followed:
for the above code, please note that: 1. for the insert, the column name should be listed out, especially the 'teacher_id' 2. any time, the 'identity_insert' can be set to 'on' for only one table, if the 'identity_insert' is set to 'on' and set that for another table, there will be error, so for tip3 3. it's suggested that every time after set the identity_insert to be 'on', set it back to 'off' after the operation
相关文章推荐
- System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'Food' when IDENTITY_INSERT is set to OFF
- Cannot insert explicit value for identity column in table 'settings' when IDENTITY_INSERT is set to OFF.
- An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON
- Cannot insert explicit value for identity column in table ‘table’ when IDENTITY_INSERT is set to OFF
- Cannot insert explicit value for identity column in table 'settings' when IDENTITY_INSERT is set to OFF.
- How to Insert Values into an Identity Column in SQL Server
- An explicit value for the identity column in table 'users' can only be specified when a column list
- java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column
- java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column
- Reset Identity Column Value in SQL Server (Identity Reset)
- java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column
- can bind a LONG value only for insert into a LONG column
- Repeated column in mapping for entity: .... should be mapped with insert="false" update="false"
- Repeated column in mapping for entity:(should be mapped with insert="false" update="false")
- SQL Server: Add column with default value and description in T-SQL
- add column for a table if this column not exists using T-SQL in SQLServer
- ORA-01461: can bind a LONG value only for insert into a LONG column
- can bind a LONG value only for insert into a LONG column
- 关于出现数据库错误ORA-01461: can bind a LONG value only for insert into a LONG column
- 关于ORA-01461: can bind a LONG value only for insert into a LONG column