0

我正在尝试从表中获取值并将它们插入到另一个表中。但是,有一个数据库列需要每次增加 1 个值。该值虽然不是标识插入列,但该值来自另一个表。还有另一个 db 列用作计数器。我写了一些东西,但它没有帮助:(121 个文件)

declare @count int;
set @count=0
while @count<=121
begin
insert into cabinet..DOCUMENT_NAMES_ROBBY (TAG,TAGORDER,ACTIVE,QUEUE,REASON,FORM,DIRECT_VIEW,GLOBAL,FU_SIGN,
SIGN_X,SIGN_Y,SIGN_W,SIGN_H,ANNOTATE,doctype_id,CODE,DOC_TYPE,SET_ID,SUSPEND_DELAY,Text_Editing,Restrict_Viewing,Viewing_Expire,
Viewing_Period,DocHdrLength,DocFtrLength,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,(select nextid from cabinet..Wfe_NextValue where Name='documents')+1, CODE,DOC_TYPE,'2',SUSPEND_DELAY,Text_Editing,Restrict_Viewing,Viewing_Expire,
Viewing_Period,DocHdrLength,DocFtrLength,DocRuleId,Outbound,SigQueue,SigReason from cabinet..document_names where SET_ID ='1'
update cabinet..Wfe_NextValue set NextID=NextID+1 where Name='documents'
set @count=@count+1
end

该数据库列是 doctype_id。上面显然是错误的,并在表中放置了 14,000 行。基本上我想从 document_names 中获取每一个条目并将其放入 document_names_robby ......除了 doctype_id 列应该从 wfe_nextvalue +1 中获取值,同时在插入下一个文档名称之前将该表中的该数字增加 1进入 document_Names_Robby。任何帮助表示赞赏

4

3 回答 3

0
declare @count int;
set @count=0
declare @nextId int;
select @nextId= nextid from cabinet..Wfe_NextValue where Name='documents'
while @count<=121
begin
insert into cabinet..DOCUMENT_NAMES_ROBBY         (TAG,TAGORDER,ACTIVE,QUEUE,REASON,FORM,DIRECT_VIEW,GLOBAL,FU_SIGN,
        SIGN_X,SIGN_Y,SIGN_W,SIGN_H,ANNOTATE,doctype_id,CODE,DOC_TYPE,SET_ID,SUSPEND_DELAY,Text_Edi    ting,Restrict_Viewing,Viewing_Expire,
Viewing_Period,DocHdrLength,DocFtrLength,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,(select nextid from cabinet..Wfe_NextValue where     Name='documents')+1,     CODE,DOC_TYPE,'2',SUSPEND_DELAY,Text_Editing,Restrict_Viewing,Viewing_Expire,
Viewing_Period,DocHdrLength,DocFtrLength,DocRuleId,Outbound,SigQueue,SigReason from     cabinet..document_names where SET_ID ='1'
set @count=@count+1
end
update cabinet..Wfe_NextValue set NextID=NextID+121 where Name='documents'
于 2013-03-16T12:48:03.370 回答
0

许多流行的数据库都支持序列。对于序列,有一个函数nextval,它返回序列值并递增序列计数器和currval,它返回最近一次返回的值,您也可以设置初始值和增量。当在表列中存储计数器不是时,序列是线程安全的。

使用序列重写您的代码。

于 2013-03-16T12:36:18.630 回答
0

假设您使用的是SQL Server数据库。使用IDENTITY功能

SELECT *, IDENTITY(int, 1,1) AS IDCol FROM Cabinet.DocumentNames INTO #Tab1 WHERE Set_Id = '1';    
insert into cabinet..DOCUMENT_NAMES_ROBBY (TAG,TAGORDER,ACTIVE,QUEUE,REASON,FORM,DIRECT_VIEW,GLOBAL,FU_SIGN,
SIGN_X,SIGN_Y,SIGN_W,SIGN_H,ANNOTATE,doctype_id,CODE,DOC_TYPE,SET_ID,SUSPEND_DELAY,Text_Editing,Restrict_Viewing,Viewing_Expire,
Viewing_Period,DocHdrLength,DocFtrLength,DocRuleId,Outbound,SigQueue,SigReason) 
SELECT * FROM #Tab1;
DROP TABLE #Tab1;
于 2013-03-16T12:37:11.600 回答