1

1 つのテーブルの XML を生成できる次のスクリプトがあります。1 つの XML クエリで 3 つの異なるテーブルから取得できるように、これを少し変更する必要があります。これを 1 つのクエリで実行するように変更する方法がわかりません。ストアド プロシージャを作成するだけでよいでしょうか。

SELECT 
CAST(EmployeeNumber AS varchar(9)) AS [employee/id], 
LastName AS [name/last], 
FirstName AS [name/first], 
'Employee' AS [persontype], 
email AS email
FROM dbo.Employees AS sd
WHERE (IsActive = 'Y') FOR XML PATH('employees')      

他のテーブルは次のとおりです。

dbo.Contractors

そのためのクエリは少し異なります。

SELECT 
CAST(EmployeeNumber AS varchar(9)) AS [employee/id], 
LastName AS [name/last], 
FirstName AS [name/first], 
'Contractor' AS [persontype], 
email AS email
FROM dbo.Employees AS sd
WHERE (IsActive = 'Y') FOR XML PATH('employees')     

最後のものは次のとおりです。

dbo.インターン

SELECT 
CAST(EmployeeNumber AS varchar(9)) AS [employee/id], 
LastName AS [name/last], 
FirstName AS [name/first], 
'Intern' AS [persontype], 
email AS email
FROM dbo.Employees AS sd
WHERE (IsActive = 'Y') FOR XML PATH('employees')     

ここで変更される唯一のフィールドは5行目です

4

2 に答える 2

1

これが役立つことを願っています:

select * from (
 SELECT 
 CAST(EmployeeNumber AS varchar(9)) AS [employee/id], 
 LastName AS [name/last], 
 FirstName AS [name/first], 
 'Employee' AS [persontype], 
 email AS email
 FROM dbo.Employees AS sd
 WHERE (IsActive = 'Y') 
 union
 SELECT 
 CAST(EmployeeNumber AS varchar(9)) AS [employee/id], 
 LastName AS [name/last], 
 FirstName AS [name/first], 
 'Contractor' AS [persontype], 
 email AS email
 FROM dbo.Employees AS sd
 WHERE (IsActive = 'Y'
 union
 SELECT 
 CAST(EmployeeNumber AS varchar(9)) AS [employee/id], 
 LastName AS [name/last], 
 FirstName AS [name/first], 
 'Intern' AS [persontype], 
 email AS email
 FROM dbo.Employees AS sd
 WHERE (IsActive = 'Y')) employees
 FOR XML PATH('employees')  
于 2013-05-06T14:52:16.513 に答える
0

UNIONこれら 3 つのクエリを実行できない理由はありますか? これにより、3 つの個別のクエリが 1 つのデータセットとして扱われます。

于 2013-05-06T14:45:42.280 に答える