随笔 - 58  文章 - 14 评论 - 10 trackbacks - 0
<2008年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

与我联系

搜索

 

常用链接

留言簿

我参加的小组

随笔档案(58)

文章分类(11)

Watch

最新评论

  • 1. re: APACHE+ASP.NET 出现问题
  • 发现原来不是apache的原因,/joe/后面的那一串字符是sessionid,在web.config中sessionState的cookieless设置为UseUri就回出现这个问题。 这几天都在...
  • --josephshi
  • 2. re: compiere/adempiere+pgsql8.2+RHEL4+jdk1.5
  • compiere是不支持postgresql的,只支持postgresql plus AD server。adempiere是支持oracle postgresql的,若在linux下使用postgr...
  • --yuzifu

阅读排行榜

评论排行榜

When columns have the identity property set on them, the value automatically increases on every row, depending on the seed and incrementing value.

So let us create a table with an identity column

CREATE TABLE [dbo].[TableA](
[ID] [int] IDENTITY(1,1NOT NULL,
[SomeName] [char](10NULL
ON [PRIMARY]

And now let us add some data in that table:

INSERT TableA(SomeName) VALUES ('A')
INSERT TableA(SomeName) VALUES ('B')
INSERT TableA(SomeName) VALUES ('C')
INSERT TableA(SomeName) VALUES ('D')

If you say

SELECT * FROM TableA ; you will see that the ID column contains value from 1 to 4.

Now let us say that the user goes ahead and deletes ID 2

DELETE FROM TableA where ID = 2 ;

If you now do a SELECT * FROM TableA

There is a gap in between ID 1 and 3. If you try to explicitly add a value to the identity column to fill up the gap using the statement:

INSERT TableA(ID,SomeName) VALUES(2,'R')

You will get an error “Cannot insert explicit value for identity column in table 'TableA' when IDENTITY_INSERT is set to OFF”

In order to insert a value into an Identity column, set Identity to ON. Use the code below to do so:

SET IDENTITY_INSERT TableA ON

INSERT TableA(ID,SomeName) VALUES(2,'R')

SET IDENTITY_INSERT TableA OFF

Setting Identity_Insert to ON removes this check. Once you have inserted the record, set it back to OFF again.

posted on 2008-04-02 14:12 josephshi 阅读(176) 评论(1)  编辑 收藏

FeedBack:
#1楼  2008-05-22 10:18 Diviner [未注册用户]
Can I do "SET INDENTITY_INSERT <TableA> ON/Off" in Visual Studio environment?
  回复  引用    

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      


相关链接: