1

I have one SQL server database which was created using Import/Export Wizard from an Access Database. I then have another SQL database which is based upon the Access database but has a slightly different schema. I am trying to get the data from the simple Access based database into the more complex SQL server schema database. The Access database simply stored the Country as text in Contact table and in my new schema the Contact table has a CountryId which links to the Country table. I've tried to write the SQL to do this:

UDPATE SQLVersion.dbo.Contact
SET CountryId = (SELECT LookupCountry.Id
                 FROM SQLVersion.dbo.Country as LookupCountry, AccessDBVersion.dbo.tblContact as AccessContact
                 WHERE LookupCountry.Name = AccessContact.Country);

This doesn't work because:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

I can understand why this error is thrown but I don't know how to form the correct SQL that will allow it to update each row with the value found from the lookup. Can anyone help please?

4

1 回答 1

4

看起来你错过了Contact和之间的关系tblContact。假设它们由 链接name,您可以以更清晰的方式重写查询,例如:

update  c
set     countryId = lc.Id
from    SQLVersion.dbo.Contact c
join    AccessDBVersion.dbo.tblContact ac
on      ac.Name = c.Name
join    SQLVersion.dbo.Country lc
on      lc.Name = ac.Country
于 2012-06-29T12:45:12.383 回答