当前位置: 代码迷 >> Sql Server >> 苦闷,select的结果加上表值函数返回结果进行过滤后就不正确
  详细解决方案

苦闷,select的结果加上表值函数返回结果进行过滤后就不正确

热度:65   发布时间:2016-04-24 09:47:54.0
郁闷,select的结果加上表值函数返回结果进行过滤后就不正确
两个表
upfiles:存的是文件,主键file_id,外键directory_id
upfiles_directory:存的是文件夹,主键directory_id

下面这一句联合查询特定的两个文件,结果正常:
select a.file_id,a.filename,b.directory_id,b.class_name
from upfiles a,upfiles_directory b 
where a.file_id in (25034,25033)
 and a.directory_id=b.directory_id


下面这个检验自定义函数的返回值,正常
select directory_id from [dbo].[根据员工编号获得权限内文件夹编号列表](164) 
where directory_id in (1924,1923)


然后将两个语句合并后,执行结果,发现少了一条记录,问题出在哪里?

------解决思路----------------------
函数是行内函数还是多语句函数?
如果是行内函数,改为多语句函数(RETURN TABLE 后有列定义)试试。
------解决思路----------------------
Create FUNCTION [dbo].[根据员工编号获得权限内文件夹编号列表]
(
@Yuangong_ID int
)
RETURNS @T TABLE (Directory_ID INT)
AS
INSERT INTO @T
select distinct a.Directory_ID from 
(select Directory_ID from upfiles_directory where 
dep_ID in (select dep_id from [dbo].[根据员工编号获得权限内部门专题编号列表](@Yuangong_ID))
and Directory_ID not in (select distinct Directory_ID from Upfiles_Directory_Power_Detail)
union all
(select Directory_ID from Upfiles_Directory_Power_Detail where Yuangong_ID=@Yuangong_ID)) a
RETURN  

如果不是INT 就把INT换成你的类型
  相关解决方案