2013-03-16 129 views
0

我想從表中取值並將它們插入到另一個表中。但是,有一個數據庫列需要每次增加1個值。該值雖然不是標識插入列,但值來自另一個表。還有另一個數據庫列作爲計數器。我寫了幾件事情,但它只是心不是幫助: (121號文件)來自另一個表循環的SQL插入語句

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 @[email protected]+1 
end 

該數據庫列是doctype_id。上面顯然出錯了,放在表中有14000行。基本上我想從document_names中取出每一個條目並將其放入document_names_robby中,除了doctype_id列應該從wfe_nextvalue +1取值,同時在該表中將該數字增加1之後再插入下一個文檔名稱放入document_Names_Robby。任何幫助表示讚賞

回答

0

許多流行的數據庫支持sequences。對於該序列,有一個函數nextval,該函數返回序列值並遞增序列計數器並返回最近一次返回的最新值,也可以設置初始值和遞增值。在表格列中存儲計數器時,序列是線程安全的。

使用序列重寫您的代碼。

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; 
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 @[email protected]+1 
end 
update cabinet..Wfe_NextValue set NextID=NextID+121 where Name='documents' 
+0

拉溫德拉,我這樣做: SELECT *,IDENTITY(INT,1,1)AS IDCol FROM Cabinet.Document_Names INTO cabinet..document_names_robby 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,'2',SUSPEND_DELAY,Text_Editing ,Restrict_Viewing,Viewing_Expire, Viewing_Period,DocHdrLength,DocFtrLength,DocRuleId,Outbound,SigQueue,SigReason) SELECT * FROM cabinet..document_names_robby; DROP TABLE cabinet..document_names_robby; – 2013-03-16 14:23:02

+0

但得到了 Msg 177,Level 15,State 1,Line 1 IDENTITY函數只能在SELECT語句有INTO子句時使用。 Msg 102,Level 15,State 1,Line 3 「2」附近的語法不正確。 和用戶留下的其他評論產生了超過14000行像我最初做 – 2013-03-16 14:23:37