与UDF交叉应用

时间:2015-05-29 13:09:37

标签: sql sql-server tsql inner-join 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的productID,这是不可能的。

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

ORDER DETAILS表和PRODCUTS表由ProductID链接

Select* from getbyID (1)  gives result below

enter image description here

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

enter image description here

你能看到错误在哪里吗?

1 个答案:

答案 0 :(得分:3)

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

@app.route("/api/users")