简体   繁体   English

SQL查询在链接服务器上运行3步(Select-Insert-delete)

[英]SQL query to run 3 step (Select-Insert-delete) on linked server

I have a linked server to mysql in SQL Server 2008. 我在SQL Server 2008中有一个到mysql的链接服务器。

I'm looking for a stored procedure to do these 3 steps: 我正在寻找一个存储过程来执行这3个步骤:

  1. select from mysql 从mysql中选择
  2. insert selected value into SQL Server 将所选值插入SQL Server
  3. delete the selected value from mysql 从mysql中删除选定的值

Before doing with linked server there was a witness machine and that machine this these steps programmatically like this : 在使用链接服务器之前,有一个见证机器和那台机器这些步骤以编程方式如下:

Pseudo code : 伪代码:

SELECT values from source
if($row>0)
    {
    foreach ($row as $rows)
    {
        INSERT INTO dest (selected value from source)
        Delete from  source
        }
    }

in programmatically way I do a loop and I know exactly when insert happen then I remove the source value, but how can I implement this fail-safe in a stored procedure and a daily job ? 以编程方式我做一个循环,我确切地知道插入发生的时间然后我删除了源值,但是如何在存储过程和日常工作中实现这个故障安全?

This is my insert code method without delete : 这是我的插入代码方法没有删除:

declare @count bigint
select @count = max(id) from dest.table

INSERT INTO dest.table(ID)
    SELECT TOP 50000 ID
    FROM [sourceLinkedServer].[db].[table] 
    WHERE ID > @count

Perhaps something like this will do the job: 也许这样的事情可以完成这项工作:

create table #temp (ID int, value varchar(20))

insert into #temp(ID,value)
select top 50000 value from [sourceLinkedServer].[db].[table]
where ID > (select max(ID) from destTable)
order by ID   

insert into destTable (value)
select value from #temp

delete ls 
from [sourceLinkedServer].[db].[table] as ls
where exists (select 1 from #temp as t where t.id = ls.ID)

drop table #temp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM