MS SQL获取带有数据的表行

时间:2021-08-25 10:17:16

I have two seperate database with the same table structure but with different data. My database "a" got a table named articles with the columns of id, heading, detail and date. Database "b" also got the identical table.

我有两个单独的数据库具有相同的表结构但具有不同的数据。我的数据库“a”有一个名为article的表,其中包含id,heading,detail和date列。数据库“b”也得到了相同的表。

For example:

a database has 10 rows.

一个数据库有10行。

b database has 12 rows.

b数据库有12行。

Is there anyway to get the 3 selected row from a and transfer it to the b with the data that rows hold.

无论如何都要从a中获取3个选定的行并使用行保存的数据将其传输到b。

2 个解决方案

#1


1  

One reasonable interpretation of the question is that you want to move rows from a.dbo.articles to b.dbo.articles that are not already there:

对该问题的一个合理解释是,您希望将行从a.dbo.articles移动到尚未存在的b.dbo.articles:

insert into b.dbo.articles(id, heading, detail, date)
    select id, heading, detail, date
    from a.dbo.articles a
    where not exists (select 1 from  b.dbo.articles b where b.id = a.id);

Notes:

  • This assumes that the two databases are on the same server. Otherwise, you need a database link.
  • 这假设两个数据库位于同一服务器上。否则,您需要一个数据库链接。

  • This assumes that the id is sufficient for the comparison. If not, just add the right logic to the where clause.
  • 这假设id足以进行比较。如果没有,只需将正确的逻辑添加到where子句。

#2


0  

Try this

Insert Into DatabaseB.MyTable (id, heading, detail date) Select id, heading, detail date From DatabaseA.MyTable Limit 3

Insert Into DatabaseB.MyTable(id,heading,detail date)从DatabaseA.MyTable Limit 3中选择id,heading,detail date

#1


1  

One reasonable interpretation of the question is that you want to move rows from a.dbo.articles to b.dbo.articles that are not already there:

对该问题的一个合理解释是,您希望将行从a.dbo.articles移动到尚未存在的b.dbo.articles:

insert into b.dbo.articles(id, heading, detail, date)
    select id, heading, detail, date
    from a.dbo.articles a
    where not exists (select 1 from  b.dbo.articles b where b.id = a.id);

Notes:

  • This assumes that the two databases are on the same server. Otherwise, you need a database link.
  • 这假设两个数据库位于同一服务器上。否则,您需要一个数据库链接。

  • This assumes that the id is sufficient for the comparison. If not, just add the right logic to the where clause.
  • 这假设id足以进行比较。如果没有,只需将正确的逻辑添加到where子句。

#2


0  

Try this

Insert Into DatabaseB.MyTable (id, heading, detail date) Select id, heading, detail date From DatabaseA.MyTable Limit 3

Insert Into DatabaseB.MyTable(id,heading,detail date)从DatabaseA.MyTable Limit 3中选择id,heading,detail date