2016-10-10 80 views
1

說我有一個這樣的查詢蒙上XML查詢在連接查詢在SQL服務器使用鑄造列名

SELECT TOP 5 
    CAST (row.query('col(/xml/tag)'), as NVARCHAR(100)) AS 'foobar' 
FROM db.table 

如何使用在LEFT JOIN XML查詢的鑄造結果?

SELECT TOP 5 
    CAST (row.query('col(/xml/tag)'), as NVARCHAR(100)) AS 'foobar' 
FROM db.table 
LEFT JOIN db.table_table 
-- don't know the syntax for this part 
ON db.table['foobar'] = db.table_table.col 
+0

這需要[動態SQL](https://msdn.microsoft.com/en-us/library/ms188001.aspx) – JohnHC

+0

@JohnHC感謝對於文檔,欣賞它 –

回答

2

爲原始查詢爲sub-select

SELECT foobar 
FROM (SELECT TOP 5 Cast (row.query('col(/xml/tag)') AS NVARCHAR(100)) AS foobar 
     FROM db.TABLE) t 
     LEFT JOIN db.table_table 
       ON t.foobar = db.table_table.col