简体   繁体   English

SQL Server 2008-函数错误

[英]SQL Server 2008 - Function error

I am new to sql programming; 我是sql编程的新手; trying to develop this function to get rows of clients who have a specific number of visits from a view: 尝试开发此功能以从视图中获取具有特定访问次数的客户行:

    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

I get the following error: 我收到以下错误:

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

I would appreciate your support. 多谢您的支持。 Thanks in advance. 提前致谢。

Your problem is here: 您的问题在这里:

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

@Count is expecting a number - you're giving it a bunch of rows, try: @Count需要一个数字-您要给它很多行,请尝试:

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

The error is telling you that your subquery is returning too many rows. 错误告诉您子查询返回太多行。 You need to return only one row is you are assigning the result to a variable. 将结果分配给变量时,只需返回一行。

Change 更改

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

To

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM