2012-02-20 26 views
1

我有一個內聯表值函數,我想從一個選擇中傳遞列,但將使用表別名。使用表別名。列名稱作爲參數調用內聯UDF

例如:

select top 1000 a.*, b.* from item a 
LEFT JOIN itemList b on a.item_id = b.item_id 
where a.item_id in (SELECT * FROM dbo.fn_GIVFUC('1234567', a.item_id)) 

結果:不正確的語法靠近 'A'。

感謝

回答

5

你不得不使用CROSS APPLY這樣

select top 1000 
    a.*, b.* 
from 
    item a 
    CROSS APPLY 
    dbo.fn_GIVFUC('1234567', a.item_id) c ON a.item_id = c.item_id 
    LEFT JOIN 
    itemList b on a.item_id = b.item_id 

這意味着你可能會得到重複的,所以這個可能工作。我無法測試

select top 1000 
    a.*, b.* 
from 
    item a 
    LEFT JOIN 
    itemList b on a.item_id = b.item_id 
WHERE 
    EXISTS (
     SELECT * 
     FROM dbo.fn_GIVFUC('1234567', a.item_id) 
     -- may need this WHERE a.item_id = c.item_id 
    ) 
+0

感謝您的快速回復,但我仍然得到同樣的錯誤:「附近有語法錯誤‘A’」 – Jdunn5 2012-02-20 16:38:29

+0

看來,sql不像我那樣將a.column名稱傳遞給函數。這是一個INLINE表函數,它返回一個表,並期待多個結果,這就是爲什麼我在 – Jdunn5 2012-02-20 16:43:35

+1

中使用的初始sql在這種情況下,請檢查數據庫兼容模式。它需要是90(或更高的SQL Server 2008+)來允許這一點。它不會在「80」中工作 – gbn 2012-02-20 16:54:45