2012-02-20 30 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