什么是该查询的目的是什么?您正在与其他人加入目标表(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
是变量存储从下拉列表数据。
您需要选择与插入相同数量的列。 – jarlh
在INSERT中选择时没有值。即'插入区域(col1,...)选择...'。 – jarlh
insert into area(column1,column2)select forcolumn1,forcolumn2 from x 。你不需要“值”,因为值来自select。 –