关于为子查询中的函数传递参数的问题!

seahi 2008-02-29 12:31:12
比如:select * from a where exists(select * from udf_b(a.userid))
为函数udf_b传递参数a.userid会报错,应该如何实现?
...全文
346 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
seahi 2008-03-20
  • 打赏
  • 举报
回复
事实证明在有些系统中运行正常,在有些系统中运行报错,难道是哪设置的问题?
是否有大侠知道呢?
seahi 2008-02-29
  • 打赏
  • 举报
回复
select * from a
inner join (select * from dbo.udf_b(a.userid)) b
也报同样的错误。
seahi 2008-02-29
  • 打赏
  • 举报
回复
Incorrect syntax near '.'. 这个点所指的就是a.userid中的这个点,如果将a.userid换成具体值或变量都正常。但传递字段名时就报错。
seahi 2008-02-29
  • 打赏
  • 举报
回复
回楼上两位,udf_b放回的是表变量,我用的是sql 2005所以不加dbo是没有关系的。
此语句执行时提示的错误是:
Msg 102, Level 15, State 1, Procedure up_query_surveys, Line 1786
Incorrect syntax near '.'.
麻烦各位高手再帮忙看看啊。
pt1314917 2008-02-29
  • 打赏
  • 举报
回复

这样???

select * from a where exists(select * from dbo.udf_b(a.userid))
fcuandy 2008-02-29
  • 打赏
  • 举报
回复
你的函数返回的是表变量?

看不到你的函数,只能说
try:

select * from a where exists(select * from dob.udf_b(a.userid) x)
pt1314917 2008-02-29
  • 打赏
  • 举报
回复
熊的代码,我在2000中执行也是报错噢。。。
seahi 2008-02-29
  • 打赏
  • 举报
回复
真奇怪了。发现在有的服务器上执行正常,有的服务器上执行出错,难道是哪设置的问题?
seahi 2008-02-29
  • 打赏
  • 举报
回复
那可真奇怪了。我将这段代码发给别人执行也提示这错误。
dobear_0922 2008-02-29
  • 打赏
  • 举报
回复
晕,偶用的就SQL2005,没有问题
seahi 2008-02-29
  • 打赏
  • 举报
回复
楼上辛苦了,奇怪了。楼上的代码我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中的用法不同?
dobear_0922 2008-02-29
  • 打赏
  • 举报
回复
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, '连续剧', 1

go
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
go

declare @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
dobear_0922 2008-02-29
  • 打赏
  • 举报
回复
估计是你的函数写得有问题

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧