我在這整個SQL數據庫遷移的東西真的很新,我準備學習。如何將數據庫表中的數據遷移到另一個SQL?
我有2個不同的數據庫,他們都有一個用戶表,在源數據庫中稱爲HtUser
在一個目標數據庫和HTUser
。
我想創建一個腳本,是複製源數據庫從特定列中的數據,這將是firstName, lastName, email
和我的目的地分貝,這將是FirstName, LastName, Email
和「專業」 UserName
包含從firstName
CH +兩個字母和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;
你到目前爲止寫了什麼? – Axarydax