sql多數(shù)據(jù)庫查詢批量插入問題循環(huán)
下文將為您介紹sql多數(shù)據(jù)庫查詢批量插入問題循環(huán)的例子,供您參考,如果您在使用SQL數(shù)據(jù)庫中遇到過類似的問題,不妨一看,相信對您會有所啟示。
從本庫查詢另外的庫,需要使用鏈接服務(wù)器
在sql2005中選服務(wù)器對像,找到鏈接服務(wù)器節(jié)點右鍵
鏈接服務(wù)器名:youlinkedserverName
服務(wù)類型 其它
訪問接口 SQL Native Client
產(chǎn)品名 youproductName
數(shù)據(jù)源 目標(biāo)數(shù)據(jù)庫服務(wù)器名或者IP
確定
儲存過程方法創(chuàng)建
EXEC master.dbo.sp_addlinkedserver @server = N'LKPU', @srvproduct=N'test', @provider=N'SQLNCLI', @datasrc=N'PU'
批量插入問題.
關(guān)鍵要解決不能重復(fù)的記錄
使用 not exists
insert into qcda(CarNumber,CarHost,CarBodyCode,engineCode)
(
select [車號],[車主姓名],[車識代碼],[發(fā)動機號] from LK.[A_01_2007].[dbo].[qcda]
where not exists(select CarNumber,CarHost,CarBodyCode from qcda where [車號]=qcda.CarNumber and [車識代碼]=qcda.CarBodyCode )
)
sql 循環(huán)
DECLARE mycursor CURSOR
FOR SELECT [name] FROM LK.[master].[sys].[databases] where [name] like 'A_%_[2,1,6,7,8,9]' --定義游標(biāo)
OPEN mycursor --打開游標(biāo)
declare @fieldname sysname --定義變量
fetch next from mycursor into @fieldname --給變量賦值
while (@@FETCH_STATUS <> -1) --判斷游標(biāo)
begin
IF (@@FETCH_STATUS <> -2)
begin
exec Import_qcda @fieldname
fetch next from mycursor into @fieldname --給變量賦值
end
end
close mycursor --關(guān)閉游標(biāo)
DEALLOCATE mycursor --銷毀游標(biāo)
【編輯推薦】