我有两张桌子:
1.
CREATE TABLE [dbo].[HotelSourceMap](
[hotelsourcemapid] [bigint] IDENTITY(1,1) NOT NULL,
[dspartnerid] [bigint] NOT NULL,
[dshotelid] [bigint] NOT NULL,
[countrycode] [varchar](5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[countryid] [bigint] NULL)
2.
CREATE TABLE [dbo].[country](
[countryId] [smallint] IDENTITY(1,1) NOT NULL,
[countryName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[countryCode] [varchar](2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL)
情况是这样的: 我想从country.countryid更新HotelSourceMap表中的countryid。 其中hotelsourcemap.countrycode = country.countrycode
有些事情是这样的:
UPDATE HotelSourceMap
SET
HotelSourceMap.countryid =country.countryId
WHERE
HotelSourceMap.countrycode = country.countryCode
答案 0 :(得分:2)
可能有一个基于集合的解决方案,在这种情况下,这将是更好的,我相信有人会发布它,但在那之前,至少这将完成这项工作:
UPDATE
HotelSourceMap
SET
countryid = (SELECT countryId FROM country WHERE country.countryCode = HotelSourceMap.countrycode)
答案 1 :(得分:1)
这是一个值得怀疑的表设计,但这里是SQL:
UPDATE HotelSourceMap
SET countryid = co.countryId
FROM HotelSourceMap AS hsm
JOIN country AS co
ON (hsm.countryCode = co.countryCode)