3
Create function  getbyID  ( @id int )
Returns table

as 
return( 
select * from Products where  

ProductID=@id+10)

上面的函数返回产品 ID 大于 10 的所有产品记录。

当与 CROSS APPLY 一起使用时,如下所示

select o.* from [Order Details] o 
CROSS APPLY getbyID(o.ProductID) P

我得到的结果是一些小于 10 的产品 ID,这是不可能的。

该示例使用随处可用的 NORTWIND 数据库示例。

ORDER DETAILS 表和 PRODCUTS 表由 ProductID 链接

Select* from getbyID (1)  gives result below

在此处输入图像描述

当 UDF 被调用(如上)结果显示一些 productID < 10

在此处输入图像描述

你能看到错误在哪里吗?

4

1 回答 1

3

如果您希望您的函数只返回 ProductID 大于 10 的产品,则应将此检查添加到 where 子句。例如:

Create function  getbyID  ( @id int )
Returns table
as 
return( 
select * from Products 
where  
ProductID=@id AND
ProductID > 10)
于 2015-05-29T13:23:15.760 回答