我有一个很大的问题。
我正在尝试将一些数据从现有表迁移到新表。我很难弄清楚如何做到以下几点:
在地址表中有两列:
AddressTable
---------------------------------------------
StateCode(nvarchar) and CountryCode(nvarchar)
两者都有州和国家代码的双字母代码。
现在在新表格中我们制作了两个外键
NewAddressTable
---------------------
StateId and CountryId
对应于两个表State和Country
StateTable has (Id,(FK)IdCountry,Name,Code)
CountryTable has (Id,Name,Code)
我要做的是基于地址表上的州和国家/地区代码,如何根据状态和代码添加替换旧表中的值和新表。
一个例子:
AddressTable
-------------
City StateCode PostalCode CountryCode
North Haven CT 06473 US
NewAddressTable
---------------
IdCountry IdState
236 8
CountryTable
---------------
Id Name Code
236 UNITED STATES US
StateTable
--------------
Id IdCountry Name Code
8 236 CONNECTICUT CT
谢谢。
答案 0 :(得分:1)
这样的事情应该有效。
insert into newtable
(idCountry, idState)
select country.id, state.id
from oldtable join country on oldtable.CountryCode = Country.Code
join state on oldtable.stateCode = state.code