T-SQL向自增字段中插入值
2009-11-28 16:03
176 查看
---以前做过这件事,但是,现在项目中碰到了,一时半会却想不起来~~~
遇到到网上搜搜,并找到http://hi.baidu.com/grrc/blog/item/16d216a85b61bbb6ca130c13.html这篇文章。
现在把该文章的内容摘抄在这里,以作备忘:
通常情况下,不能向 SQL Server 自增字段插入值,如果非要这么干的话,SQL Server 就会好不客气地给你个错误警告:
这个错误消息提示我们,如果向 SQL Server 自增字段插入值,需要设置 identity_insert 选项为 on。
看具体的一个例子:
注意的是,自增字段插入值后,要及时把 identity_insert 设置为 off。
遇到到网上搜搜,并找到http://hi.baidu.com/grrc/blog/item/16d216a85b61bbb6ca130c13.html这篇文章。
现在把该文章的内容摘抄在这里,以作备忘:
通常情况下,不能向 SQL Server 自增字段插入值,如果非要这么干的话,SQL Server 就会好不客气地给你个错误警告:
Server: Msg 544, Level 16, State 1, Line 1 Cannot insert explicit value for identity column in table 't' when identity_insert is set to OFF.
这个错误消息提示我们,如果向 SQL Server 自增字段插入值,需要设置 identity_insert 选项为 on。
set identity_insert on
看具体的一个例子:
create table dbo.t ( id int identity(1,1) not null, name varchar(50) )
set identity_insert t on insert into t (id, name) values(1, 'sqlstudy') set identity_insert t off
注意的是,自增字段插入值后,要及时把 identity_insert 设置为 off。
相关文章推荐
- oracle+mybatis 使用动态Sql当插入字段不确定的情况下实现批量insert
- 后台的SQL语句插入Blob字段数据
- sql实现从两个表获取字段组成表数据再插入到函数表中
- 对于sql字段非空但插入值为空,将值转换为空的字符串
- Sql 把一个表中某个字段的数据插入到另一个表中
- oracle+mybatis 使用动态Sql当插入字段不确定的情况下实现批量insert
- db2插入 timestamp 类型字段 的sql
- sql 连接表的两个字段拼接成新字段 ,形成新字段插入
- mysql sql 语句插入 blob字段的插入
- sql自增字段插入数据
- mysql 一怪sql语句:插入字段(uid,username,level,time...),且username是据uid从另表查出,其它为常数
- 一条SQL语句插入大量数据 和查看某个表的字段是否有重复值
- SQL语句实现不存在即插入,存在则increase某字段的功能insert into … on duplicate key update
- 向已写好的多行插入sql语句中添加字段和值
- 如何用SQL语句在指定字段前面插入新的字段?
- 解决SQL插入EXCEL字段顺序错乱问题
- 拼组SQL中,Oracle插入超4000字节的CLOB字段的处理方法
- 在编写触发器事件时如何获得当前插入记录的字段值?(MS SQL Server)
- 如何通过sql的insert语句插入大量字符串到oracle的clob字段?