下文将为您介绍sql多数据库查询批量插入问题循环的例子,供您参考,如果您在使用SQL数据库中遇到过类似的问题,不妨一看,相信对您会有所启示。

下文将为您介绍SQL多数据库查询批量插入问题循环的例子,供您参考,如果您在使用SQL数据库中遇到过类似的问题,不妨一看,相信对您会有所启示。

sql多数据库查询批量插入问题循环(数据库批量操作)  SQL 查询 循环 第1张

从本库查询另外的库,需要使用链接服务器

在sql2005中选服务器对像,找到链接服务器节点右键

链接服务器名:youlinkedserverName

服务类型 其它

访问接口 SQL Native Client

产品名 youproductName

数据源 目标数据库服务器名或者IP

确定

储存过程方法创建

EXEC master.dbo.sp_addlinkedserver @server = N'LKPU', @srvproduct=N'test', @provider=N'SQLNCLI', @datasrc=N'PU'

批量插入问题.

关键要解决不能重复的记录

使用 not exists

insert into qcda(CarNumber,CarHost,CarBodyCode,engineCode)
(
select [车号],[车主姓名],[车识代码],[发动机号] from LK.[A_01_2007].[dbo].[qcda]
where not exists(select CarNumber,CarHost,CarBodyCode from qcda where [车号]=qcda.CarNumber and [车识代码]=qcda.CarBodyCode )
)

sql 循环

DECLARE mycursor CURSOR
FOR SELECT [name] FROM LK.[master].[sys].[databases] where [name] like 'A_%_[2,1,6,7,8,9]' --定义游标
OPEN mycursor --打开游标
declare @fieldname sysname --定义变量
fetch next from mycursor into @fieldname --给变量赋值
while (@@FETCH_STATUS <> -1) --判断游标
begin
IF (@@FETCH_STATUS <> -2)
begin
exec Import_qcda @fieldname
fetch next from mycursor into @fieldname --给变量赋值
end
end
close mycursor --关闭游标
DEALLOCATE mycursor --销毁游标

【编辑推荐】

SQL中游标嵌套循环的示例

sql循环插入的语句写法

SQL循环语句的妙用

SQL循环语句示例

SQL数据库中的临时表详解

转载请说明出处
知优网 » sql多数据库查询批量插入问题循环(数据库批量操作)

发表评论

您需要后才能发表评论