比如:select * from a where exists(select * from udf_b(a.userid))
为函数udf_b传递参数a.userid会报错,应该如何实现?

解决方案 »

  1.   

    你的函数返回的是表变量?看不到你的函数,只能说
    try:select   *   from   a   where   exists(select   *   from   dob.udf_b(a.userid) x) 
      

  2.   


    这样???select   *   from   a   where   exists(select   *   from   dbo.udf_b(a.userid)) 
      

  3.   

    回楼上两位,udf_b放回的是表变量,我用的是sql 2005所以不加dbo是没有关系的。
    此语句执行时提示的错误是:
    Msg 102, Level 15, State 1, Procedure up_query_surveys, Line 1786
    Incorrect syntax near '.'.
    麻烦各位高手再帮忙看看啊。
      

  4.   

    Incorrect   syntax   near   '.'. 这个点所指的就是a.userid中的这个点,如果将a.userid换成具体值或变量都正常。但传递字段名时就报错。
      

  5.   

    select * from a  
    inner join (select * from dbo.udf_b(a.userid)) b
    也报同样的错误。
      

  6.   

    create table tb(flid int, flmc varchar(16), pid int)
    insert tb select 1, '电影', 0
    union all select 2, '国产', 1
    union all select 3, '大陆', 2
    union all select 4, '南宁', 3
    union all select 5, '桂林', 3
    union all select 6, '连续剧', 1go
    create function fn_GetParents(@ID int)
    returns @tb table(flid int, flmc varchar(16), pid int) as
    begin
    select @ID=pid from tb where flid=@ID
    while @ID<>0
    begin
    insert @tb select * from tb where flid=@ID
    select @ID=pid from tb where flid=@ID
    end
    return 
    end
    godeclare @ID int, @pID int
    set @ID=4
    select * from dbo.fn_GetParents(@ID)
    order by flid--这一句与你的类似,执行结果正常,不存在语法错误
    select * from tb 
    where exists (select * from dbo.fn_GetParents(tb.flid))/*
    flid        flmc             pid
    ----------- ---------------- -----------
    1           电影               0
    2           国产               1
    3           大陆               2(3 row(s) affected)flid        flmc             pid
    ----------- ---------------- -----------
    2           国产               1
    3           大陆               2
    4           南宁               3
    5           桂林               3
    6           连续剧              1(5 row(s) affected)
    */
    drop function dbo.fn_GetParents
    drop table tb
      

  7.   

    楼上辛苦了,奇怪了。楼上的代码我COPY过去执行提示同样的错误,无法正常执行。(6 row(s) affected)
    Msg 102, Level 15, State 1, Line 9
    Incorrect syntax near '.'.错误是指向这句:
    select * from tb 
    where exists (select * from dbo.fn_GetParents(tb.flid))奇怪了。难道是SQL SERVER 2005中与2000中的用法不同?
      

  8.   

    晕,偶用的就SQL2005,没有问题