与 UDF 交叉应用
CROSS APPLY WITH UDF
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 table 和 PRODCUTS table 由 ProductID
链接
Select* from getbyID (1) gives result below
调用 UDF 时(如上)结果显示某些 productID < 10
你能看出错误在哪里吗?
如果您希望您的函数仅 return ProductID 大于 10 的产品,您应该将此检查添加到 where 子句中。例如:
Create function getbyID ( @id int )
Returns table
as
return(
select * from Products
where
ProductID=@id AND
ProductID > 10)
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 table 和 PRODCUTS table 由 ProductID
链接Select* from getbyID (1) gives result below
调用 UDF 时(如上)结果显示某些 productID < 10
你能看出错误在哪里吗?
如果您希望您的函数仅 return ProductID 大于 10 的产品,您应该将此检查添加到 where 子句中。例如:
Create function getbyID ( @id int )
Returns table
as
return(
select * from Products
where
ProductID=@id AND
ProductID > 10)