1

我是 sql 编程新手;尝试开发此功能以从视图中获取具有特定访问次数的客户行:

    ALTER FUNCTION [dbo].[fn_NumberOfVisit] 
(
@nv int
)
RETURNS varchar(500)
AS
BEGIN
DECLARE @ret varchar(500)

 select *
from (
    select 
        *,
        rn = row_number() over (partition by ClientId order by VisitId)
    from
        Visit
) activityWithRn
inner join vw_MasterView on  vw_MasterView.VisitId = activityWithRn.VisitId
where activityWithRn.rn =@nv

RETURN @ret

END

我收到以下错误:

   Select statements included within a function cannot return data to a client

感谢您的支持。提前致谢。

4

2 回答 2

1

你的问题在这里:

set @Count = ( select *
from (
    select 
        *,

@Count 期待一个数字 - 你给它一堆行,试试:

set @Count = ( select Count(*)
from (
    select 
于 2012-05-23T21:57:17.360 回答
0

该错误告诉您您的子查询返回了太多行。如果要将结果分配给变量,则只需要返回一行。

改变

set @Count = ( select *
from (
    select 
        *,
        rn = row_number() over (partition by ClientId order by VisitId)
    from
        Visit

set @Count = ( select count(*)
from (
    select 
        *,
        rn = row_number() over (partition by ClientId order by VisitId)
    from
        Visit
于 2012-05-23T21:59:18.190 回答