解决方案 »

  1.   

    补充:数据库是sqlserver 2000
      

  2.   

    我这边模拟了下没有问题~换EXISTS试下
    AND EXISTS(SELECT 1 FROM [dbo].[根据员工编号获得权限内文件夹编号列表](164) WHERE a.directory_id=directory_id)
      

  3.   

    换成楼上的EXISTS也不行,还是只有一条记录
    如果吧表值函数的结果插入临时表,在查询就是正确的:IF object_id(N'tempdb..#tmp1') IS NOT NULL
    DROP TABLE #tmp1
    select * into #tmp1 from [dbo].[根据员工编号获得权限内文件夹编号列表](164)
    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
     and a.directory_id in (select directory_id from #tmp1)
      

  4.   

    函数是行内函数还是多语句函数?
    如果是行内函数,改为多语句函数(RETURN TABLE 后有列定义)试试。
      

  5.   

    表值函数是这样写的,里面还引用了另外的表值函数,貌似已经是楼上说的多语句函数了Create FUNCTION [dbo].[根据员工编号获得权限内文件夹编号列表]
    (
    @Yuangong_ID int
    )
    RETURNS TABLE 
    AS
    RETURN 
    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
      

  6.   

    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换成你的类型
      

  7.   

    都是int,directory_id是自动增长的主键,在数据表中的类型也是int
      

  8.   

    我猜想有几种可能,
    你的表值函数返回的是语句查询,因为SQL有一个优化机制,它可能直接把这个查询连接到外面的查询,而不是返回这个查询的值,然后跟外面连接查询,
    这样,可能第一条查询,执行函数得到的值是1924,外面就查出一条,然后第二查询的时候,就没有再执行这个函数,而直接取上次得到的1924,这样第二条就不匹配~
    最终结果就只有一条以上仅是根据优化机制进行的猜想~~PS:查询是表乘起来的积一条一执行,一条一条判断合不合格的
      

  9.   

    太深奥了,^_^,没有CSDN日子怎么过哇
      

  10.   

    #7 是行内函数,#8 才是多语句函数。
    虽然问题解决了,但是我怀疑你是否补丁没打。
    找了台 SQL Server 2000,行内函数结果是正确的。
    CREATE FUNCTION f_numbers(@min INT,@max INT)
    RETURNS TABLE
    AS
    RETURN (
        SELECT NUMBER
         FROM master..spt_values
        WHERE TYPE = 'p'
          AND NUMBER BETWEEN @min AND @max
    )
    GOSELECT *
      FROM (
            SELECT 1 AS n UNION ALL
            SELECT 3 UNION ALL
            SELECT 5
           ) A
     WHERE n in (SELECT number FROM dbo.f_numbers(2,10))
    n           
    ----------- 
              3 
              5