高手看看这样为沙不行。。。在select中使用多语句表值函数中调用的问题?

ZHANGWEI15 2005-06-28 03:18:33
高手看看这样为沙不行。。。在多语句表值函数中调用的问题?


我写了个多语句表值函数fn(ID)用于返回多个值?

这样调用没问题

select ID,name,flag,Type from dbo.fn('123')



但是如果在表中如下调用的话就会抱错


select
ID,
(select flag from dbo.fn(customer.id)) as Flag
from customer

由于fn函数要进行一系列的判断和计算,为了处理方面所以写成了一个函数。
请问该如何解决这个问题,难道多语句表值函数不能实现吗?
如果要实现类似的功能该怎么办呢??

谢谢执教!!!
...全文
241 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZHANGWEI15 2005-06-29
  • 打赏
  • 举报
回复
你指的是在函数中加码?

我加了不行的
ReViSion 2005-06-28
  • 打赏
  • 举报
回复
在前面加上TOP 1 不知道行不行、
ZHANGWEI15 2005-06-28
  • 打赏
  • 举报
回复
多谢楼上。可是我的问题是
SELECT OD.OrderID, OD.ProductID, fnPr.Price
FROM OrderDetails as OD, fn_Products('Discontinued') AS fnPr
WHERE OD.ProductID = fnPr.ProductID
ORDER BY OD.OrderID, OD.ProductID

其中fn_Products('Discontinued')中的参数需要来自表 OrderDetails 中的某一列,请问能实现吗?
vivianfdlpw 2005-06-28
  • 打赏
  • 举报
回复
唤醒调用返回表数据类型的用户定义函数
可唤醒调用返回表 (table) 的用户定义函数,其中在 SELECT、INSERT、UPDATE 或 DELETE 语句的 FROM 子句中允许表表达式。可在调用返回表的用户定义函数后加上可选的表别名。下例说明如何唤醒调用函数 fn_Products 和指派别名:

SELECT OD.OrderID, OD.ProductID, fnPr.Price
FROM OrderDetails as OD, fn_Products('Discontinued') AS fnPr
WHERE OD.ProductID = fnPr.ProductID
ORDER BY OD.OrderID, OD.ProductID

当在子查询的 FROM 子句中唤醒调用返回表的用户定义函数时,函数参数不能引用外部查询中的任何列。

若 SELECT 语句的 FROM 子句引用了返回表的用户定义函数,则静态的只读游标是可在该 SELECT 语句上打开的唯一游标类型。

引用返回表 (table) 的用户定义函数的 SELECT 语句唤醒调用该函数一次。

ZHANGWEI15 2005-06-28
  • 打赏
  • 举报
回复
select
ID,
(select flag from dbo.fn(customer.id)) as Flag
from customer

报customer附近有错 !

但是如果像下面这样就没问题,所以我怀疑是不是sqlserver不支持用字段

select
ID,
(select flag from dbo.fn(100)) as Flag
from customer


郁闷阿!!!!!!!!!!!!!!
paoluo 2005-06-28
  • 打赏
  • 举报
回复
哦,明白叻,不好意思,我之前理解错了。

你最开始的语句报的错误是什么??
ZHANGWEI15 2005-06-28
  • 打赏
  • 举报
回复
我是想取得fn_FindReports某个field的值
ZHANGWEI15 2005-06-28
  • 打赏
  • 举报
回复
我举的只是一个例子

实际的应用是
一个id返回多个值,这些值在一个表变量里

微软的参考如下:

C. 多语句表值函数
假设有一个表代表如下的层次关系:

CREATE TABLE employees (empid nchar(5) PRIMARY KEY,
empname nvarchar(50),
mgrid nchar(5) REFERENCES employees(empid),
title nvarchar(30)
)

表值函数 fn_FindReports(InEmpID) 有一个给定的职员ID,它返回与所有直接或间接向给定职员报告的职员相对应的表。该逻辑无法在单个查询中表现出来,不过可以实现为用户定义函数。

CREATE FUNCTION fn_FindReports (@InEmpId nchar(5))
RETURNS @retFindReports TABLE (empid nchar(5) primary key,
empname nvarchar(50) NOT NULL,
mgrid nchar(5),
title nvarchar(30))
/*Returns a result set that lists all the employees who report to given
employee directly or indirectly.*/
AS
BEGIN
DECLARE @RowsAdded int
-- table variable to hold accumulated results
DECLARE @reports TABLE (empid nchar(5) primary key,
empname nvarchar(50) NOT NULL,
mgrid nchar(5),
title nvarchar(30),
processed tinyint default 0)
-- initialize @Reports with direct reports of the given employee
INSERT @reports
SELECT empid, empname, mgrid, title, 0
FROM employees
WHERE empid = @InEmpId
SET @RowsAdded = @@rowcount
-- While new employees were added in the previous iteration
WHILE @RowsAdded > 0
BEGIN
/*Mark all employee records whose direct reports are going to be
found in this iteration with processed=1.*/
UPDATE @reports
SET processed = 1
WHERE processed = 0
-- Insert employees who report to employees marked 1.
INSERT @reports
SELECT e.empid, e.empname, e.mgrid, e.title, 0
FROM employees e, @reports r
WHERE e.mgrid=r.empid and e.mgrid <> e.empid and r.processed = 1
SET @RowsAdded = @@rowcount
/*Mark all employee records whose direct reports have been found
in this iteration.*/
UPDATE @reports
SET processed = 2
WHERE processed = 1
END

-- copy to the result of the function the required columns
INSERT @retFindReports
SELECT empid, empname, mgrid, title
FROM @reports
RETURN
END
GO

-- Example invocation
SELECT *
FROM fn_FindReports('11234')
GO




paoluo 2005-06-28
  • 打赏
  • 举报
回复
回复人: ZHANGWEI15(小新) ( ) 信誉:84 2005-06-28 15:36:00 得分: 0


select
ID,
dbo.fn(customer.id) as Flag
from customer

不行的 fn(id)会返回多个值的!!!!!!!!!!!!!!!!


如果是单值得就没必要返回表了
!!!



-------------------------
那你的意思是说,同样的id就会返回多个id啦。

这样用返回表同样有错误啦。

但你又说返回的是一行,那么意思是说,返回的多个flag,只取其中一个吗??
ZHANGWEI15 2005-06-28
  • 打赏
  • 举报
回复
select
ID,
dbo.fn(customer.id) as Flag
from customer

不行的 fn(id)会返回多个值的!!!!!!!!!!!!!!!!


如果是单值得就没必要返回表了
!!!
paoluo 2005-06-28
  • 打赏
  • 举报
回复
那就试试这个

select
ID,
dbo.fn(customer.id) as Flag
from customer
ZHANGWEI15 2005-06-28
  • 打赏
  • 举报
回复
我返回的的确是一行
肯定是一行
但是不行啊!!!


paoluo 2005-06-28
  • 打赏
  • 举报
回复
你要的结果,是怎样的??

应该可以改写的。
paoluo 2005-06-28
  • 打赏
  • 举报
回复
如果这个
select flag from dbo.fn(customer.id)
返回是多个值的话,这里是表名也同样会出错的。

34,594

社区成员

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

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