简体   繁体   中英

SQL Database Migration : match existing columns, if not exists in new database - create new columns

I have a table of information relating to addresses I want to take from my current database to a new one. I want to match Addresses on Line1, Line2, and Postcode, and if any don't match I wish to create a new address in the database I am migrating to.

So far I have a staging database I import all the data from the initial database, I then have to write a SQL script to be ran before importing into the new database.

So far I have:

Use Staging_DB
GO

IF EXISTS (SELECT 1 FROM sys.tables WHERE name = 'Address'
DROP TABLE Address

CREATE TABLE Address(
AddressLine1 varchar(60),
AddressLine2 varchar(60),

.... and so on

Any help would be greatly apprceiated

If I understand correctly, you want to insert new addresses into an existing table -- but only addresses that don't exist.

If so:

insert into address (line1, line2, postcode)
    select line1, line2, postcode
    from staging s
    where not exists (select 1
                      from address a
                      where a.line1 = s.line1 and a.line2 = s.line2 and a.postcode = s.line2
                     );

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