下面是使用Select进行插入的SQL查询,它不起作用,请帮助解决问题:
insert into Area Values (AreaId, AreaName, Pincode, CityId, StateId, CountryId)
select CityId
from Area
join City On Area.CityId = City.CityId
Join State on Area.StateId = State.StateId
join Country on Area.CountryId = Country.CountryId;
答案 0 :(得分:0)
该查询的目的是什么?您正在与其他人一起加入目标表(Area
)并尝试在其中插入,这是数据已在Area
表中显示的标记,并且您正在插入重复项或尝试更新空列现在。在这种情况下,您需要合并甚至更新。
UPDATE A
SET CityID = c.CityID,
StateID = s.StateID,
CountryID = co.CountryID
from Area a
join City c
On a.CityId = c.CityId
Join State s
on a.StateId = s.StateId
join Country co
on a.CountryId = co.CountryId;
如果您需要完全插入 - 请指定所有列,如下所示:
insert into Area Values (AreaId, AreaName, Pincode, CityId, StateId, CountryId)
select a.AreaID,
a.AreaName,
a.Pincode,
c.CityID,
s.StateID,
co.CountryID
from Area a
join City c
On a.CityId = c.CityId
Join State s
on a.StateId = s.StateId
join Country co
on a.CountryId = co.CountryId;
修改强>
insert into Area Values (AreaId, AreaName, Pincode, CityId, StateId, CountryId)
select a.AreaID,
a.AreaName,
a.Pincode,
c.CityID,
s.StateID,
co.CountryID
from Area a
LEFT join City c
On c.CityName = @CityName
LEFT Join State s
on s.StateName = @StateName
LEFT join Country co
on co.CountryName = @CountryName;
@CityName
,@StateName
,@CountryName
是存储下拉列表数据的变量。