views:

34

answers:

2

i have two tables:

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)

situation is like this: i want to update countryid in HotelSourceMap table from country.countryid . where hotelsourcemap.countrycode = country.countrycode

some thing like this:

UPDATE HotelSourceMap
SET
HotelSourceMap.countryid =country.countryId  
WHERE
HotelSourceMap.countrycode = country.countryCode
+2  A: 

There's probably a set-based solution for this, in which case that would be preferable, and I'm sure someone will post it, but until then, at least this will do the job:

UPDATE
    HotelSourceMap
SET
    countryid = (SELECT countryId FROM country WHERE country.countryCode = HotelSourceMap.countrycode)
David Hedlund
wow..very good query..
Rajesh Rolen- DotNet Developer
+1  A: 

This is really questionable table design, but here is the SQL:

UPDATE    HotelSourceMap
SET       countryid = co.countryId
FROM      HotelSourceMap AS hsm
    JOIN  country        AS co
        ON (hsm.countryCode = co.countryCode)
Ryan
thanks for your solution.. its working fine. please tell me what is problem in this query...UPDATE HotelSourceMapSET hsm.hotelid = co.hotelid FROM HotelSourceMap AS hsm JOIN hotels AS co ON (hsm.hotelname= co.[name] AND hsm.cityid = co.cityid)its giving me error:The multi-part identifier "hsm.hotelid" could not be bound.
Rajesh Rolen- DotNet Developer
Try removing 'hsm.' after the SET keyword. You can only update one table at a time (which is specified after UPDATE <TableName>), so it isn't necessary to use the table alias 'hsm' in the SET clause. Also, change the alias from 'co' if you need to join a different table, e.g. 'Hotels AS ho'
Ryan