我对整个 SQL DB 迁移的东西真的很陌生,我已经准备好学习了。
我有 2 个不同的数据库,它们都有一个用户表,HtUser
在一个目标数据库和HTUser
源数据库中调用。
我想创建一个脚本,该脚本从源数据库中的特定列firstName, lastName, email
和目标数据库中的特定列复制数据,FirstName, LastName, Email
并且“特殊字段”UserName
包含 ch + 两个字母firstName
来自lastName
.
INSERT INTO HolidayTracker.dbo.HtUser(FirstName,LastName,Email,UserName,AdAccount,AdDomain)
SELECT firstName,lastName,email,'CH'+LEFT(firstName,2)+LEFT(lastName,3),'EUROPE\'+LEFT(firstName,2)+LEFT(lastName,3),''
FROM HolidayTracker.dbo.HTUser U1
WHERE NOT EXISTS ( SELECT 1 FROM HolidayTracker.dbo.HTUser U2 WHERE U2.FirstName = U1.firstName AND U2.LastName = U1.lastName AND U2.email = U1.Email);
INSERT INTO HolidayTracker.dbo.HtUser(FirstName,LastName,Email,UserName,AdAccount,AdDomain)
SELECT firstName,lastName,email,'CH'+LEFT(firstName,2)+LEFT(lastName,3),'EUROPE\'+LEFT(fistName,2)+LEFT(lastName,3),''
FROM HolidayTracker.dbo.HTUser u1
LEFT JOIN HolidayTracker.dbo.HtUser u2 ON u1.firstName = u2.FirstName AND u1.lastName = u2.LastName AND u1.email = u2.Email
WHERE u2.FirstName IS NULL
AND u2.LastName IS NULL
AND u2.Email IS NULL;