Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2005 增加现有数据库列_Sql Server 2005_Insert_Auto Increment_Identity - Fatal编程技术网

Sql server 2005 增加现有数据库列

Sql server 2005 增加现有数据库列,sql-server-2005,insert,auto-increment,identity,Sql Server 2005,Insert,Auto Increment,Identity,我试图制作一个表的精确副本,但更改了两列的值。基本上,我在db表中有800个条目,还需要800个条目,但我需要其中一个列的值Set_id为11,我需要另一个列的值自动递增…但不幸的是,该列不是identity insert。我创建了一个临时表,并将现有表复制到临时表中。然后,我尝试将temp表写回原始表,其中一列为11,并尝试使doctype id列以860开头,然后每个条目以1自动递增。我创建了这个游标: declare @id int, @document char(30) select @

我试图制作一个表的精确副本,但更改了两列的值。基本上,我在db表中有800个条目,还需要800个条目,但我需要其中一个列的值Set_id为11,我需要另一个列的值自动递增…但不幸的是,该列不是identity insert。我创建了一个临时表,并将现有表复制到临时表中。然后,我尝试将temp表写回原始表,其中一列为11,并尝试使doctype id列以860开头,然后每个条目以1自动递增。我创建了这个游标:

declare @id int, @document char(30)
select @id = 860
declare documents cursor for
/* This will select all Non-Physician users */
  select tag from cabinet..document_names

open documents

fetch next from documents into @document
while @@fetch_status <> -1
begin
 select @id = @id +1
      if not exists (select * from cabinet..document_names where set_id='11' and tag=@document)
      insert into cabinet..document_names (TAG, TAGORDER, ACTIVE,QUEUE, REASON, FORM,DIRECT_VIEW,GLOBAL,FU_SIGN,SIGN_X,
SIGN_Y,SIGN_W,SIGN_H,ANNOTATE,doctype_id,CODE,CALCTABLE_ID, DOC_TYPE,SET_ID,SUSPEND_DELAY,Text_Editing,Restrict_Viewing,Viewing_Expire,
Viewing_Period , DocHdrLength,DocHdrSearchString,DocFtrLength,DocFtrPageNo,DocRuleId,Outbound,SigQueue,SigReason)
select TAG, TAGORDER, ACTIVE,QUEUE, REASON, FORM,DIRECT_VIEW,GLOBAL,FU_SIGN,SIGN_X,
SIGN_Y,SIGN_W,SIGN_H,ANNOTATE,@ID,CODE,CALCTABLE_ID, DOC_TYPE,'11',SUSPEND_DELAY,Text_Editing,Restrict_Viewing,Viewing_Expire,
Viewing_Period , DocHdrLength,DocHdrSearchString,DocFtrLength,DocFtrPageNo,DocRuleId,Outbound,SigQueue,SigReason from
cabinet..document_names_temp 


fetch next from documents into @document

end
close documents 
deallocate documents
它正做着我想要的事情,除了doctype id重复它自己为861、861等等。我需要在原始表中的每个条目之后,该数字增加1。任何关于我搞砸的地方的帮助都是值得的!谢谢

不应该

  INSERT INTO cabinet..document_names ([columns])
  SELECT [columns] 
  FROM cabinet..document_names_temp 
还有WHERE语句吗?差不多

  INSERT INTO cabinet..document_names ([columns])
  SELECT [columns] 
  FROM cabinet..document_names_temp 
  WHERE TAG = @Document

目前,您的insert只插入文档_names_temp中的每条记录,doctype_id设置为@id,这将解释861861861的重复。。您可能也有相同数量的DOCTYPE_id 862记录。

我正在使用SQL server management studio 2005我更接近了一步…谢谢Byte…我按照您所说的做了,所以编码并将where子句放在末尾…它起作用了,有点,doctypeid出于某种原因增加了2,所以860,然后862,然后864,等等…我有select@id=@id+1在错误的位置?我试着移动它,但无法让它静止不动。我唯一能想象的是,您创建的光标包含两次每个标记。select@id=@id+1是可以的,但如果光标中有重复项,则可以解释为什么它会向每个id添加2个。每个标记有2个增量和1个insert…每个标记是什么意思。我不确定你是不是在说下一次从文件中取回文件into@document部分我拿了一个,但还是一样。奇怪的是,它从860到862和864,但没有填写861 863等等,我的意思是,声明文档光标用于从文件柜中选择标记。文档名称生成一个带有重复项的光标,我说标记,因为您在那里选择了标记。假设光标包含[tag1,tag1,tag2,tag2]。处理第一个tag1,@Id递增并插入tag1,处理第二个tag1,@Id递增但未插入,因为它已存在于目标表中。。。。我并不是说这就是正在发生的事情,但这是我唯一能想到现在正在发生的事情。啊,我明白了!!我把它改为声明文件光标,用于从文件柜中选择文件名。文件名和瞧,它工作得很好…非常感谢!!!我感谢你的跟进和帮助!