SQLServer 2008 XML 機能をテストするために、2 つのテーブルを持つ新しいデータベースを作成しました。
目的の動作を得ることができた場合、約 50 のテーブルを作成してから、新しいプロジェクト用に大量の XML ファイルをインポートする必要があります。
私のテスト環境は次のように作成されています。
create table Employees(
    idEmployeeFeed bigint primary key IDENTITY(1,1),
    EmployeeFeed xml
)
go
create table GeoCountries(
    CountriesFeed xml
)
go
次に、Employees テーブルに約 1000 個の xml ファイルをロードし、GeoCountries テーブルに 1 つのファイルをロードしました。GeoCountries ファイルには、249 か国の緯度/経度重心座標に加えて、国名と ISO 3 文字の国コードが含まれています。
各従業員には国コードがあります。すでに別の製品で XQuery を準備していましたが、クライアントの要件ごとに SQL Server に移行する必要があります。
XQuery で取得した 2 つのテーブルのサンプル データ:
select EmployeeFeed.query('//employee') as employee
from Employees
/* output:
    <employee empID="1111" >
      <displayName>John</displayName>
      <country code="USA" />
    </employee>
    <employee empID="2222" >
      <displayName>Mario</displayName>
      <country code="ITA" />
    </employee>
    ...
*/
select EmployeeFeed.query('//employee/country') as employee
from Employees
/* output:
    <country code="USA" />
    <country code="ITA" />
    ...
*/
select CountriesFeed.query('//country')
from GeoCountries
/* output:
    <country>
      <ISO3166A3>USA</ISO3166A3>
      <ISOen_name>United States</ISOen_name>
      <latitude>38.000</latitude>
      <longitude>-97.000</longitude>
    </country>
    <country>
      <ISO3166A3>ITA</ISO3166A3>
      <ISOen_name>Italy</ISOen_name>
      <latitude>42.833</latitude>
      <longitude>12.833</longitude>
    </country>
    ...
*/
select CountriesFeed.query('//country/ISO3166A3')
from GeoCountries
/* output:
    <ISO3166A3>USA</ISO3166A3>
    <ISO3166A3>ITA</ISO3166A3>
    ...
*/
これは私が実行しようとしているクエリです:
select EmployeeFeed.query(N'
  let $ccc := //country
  let $ttt := //employee
  for $t in $ttt  
  return
        <geoEmp
        empCode="{ $t/@empID }" 
        countryCode="{ $t/country/@code }" 
        latit="{ $ccc[ISO3166A3 = $t/country/@code]/latitude/text() }" 
        longit="{ $ccc[ISO3166A3 = $t/country/@code]/longitude/text() }"
        />
') as GeoEmployees
from Employees
/* output:
    <geoEmp empCode="1111" countryCode="USA" latit="" longit="" />
    <geoEmp empCode="2222" countryCode="ITA" latit="" longit="" />
    ...
*/
ご覧のとおり、国コード + 緯度/経度は変数 $ccc に事前にロードされてルックアップ テーブルとして使用されますが、そのデータは別の SQL テーブル (GeoCountries) にあるため、現在の XQuery コンテキストでは見つけることができません。 SQL テーブル Employees に関連付けられています。
別の SQL テーブルに格納されている XML にアクセスする XQuery を実行する方法はありますか? 移行する場合、100 ~ 200 の同様の状況に対処する必要があり、この問題の効率的な解決策を見つける必要があります。