简体   繁体   中英

MS SQL Get Table Row With Data

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.

For example:

a database has 10 rows.

b database has 12 rows.

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

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:

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.

尝试这个

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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