我在SQL Server 2016中有以下设置。我想在表B中插入姓名、姓名和地址到这三个列中,但是在视图A中的3列中,我想根据每个人的就业情况在表B中的就业状态列中为每个人插入一行,最好的方法是什么?
视图A
Name (String)
Surname (Sring)
Address (String)
Employed (boolean)
Non-Employed (boolean)
Retired (boolean)表B
Name (String)
Surname (Sring)
Address (String)
Employed Status (String)发布于 2019-08-16 16:50:27
尽管您有三个看似独占的标志,但我没有理由认为它们实际上是独占的(除非您有某种类型的验证)。
因此,我将把这些状态连接在一起:
insert into b (name, surname, address, employed_status)
select name, surname, address,
stuff(concat(case when employeed = 1 then ', employed' else '' end,
case when non_employeed = 1 then ',non_employed' else '' end,
case when retired = 1 then ',retired' else '' end
), 1, 1, ''
)
from a;注意: SQL Server没有boolean类型,因此我假定您对列使用0/1编码。
发布于 2019-08-16 17:03:15
我将后退一步,创建另一个名为EmploymentState的表,在其中添加三行"1 -受雇“、"2 -失业”、"3 -退休“。然后从表A和表B中向该表添加一个外键。
当然,除非一个人可以在多个state...in中,在这种情况下,您需要交叉表。请参阅https://www.bing.com/search?q=sql+intersection+table&qs=n&form=QBRE&sp=-1&ghc=1&pq=sql+intersection+tab&sc=0-20&sk=&cvid=153E23C3D25F461E89F71E106FBF7D66
https://stackoverflow.com/questions/57521226
复制相似问题