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

このエラーは、サブクエリが返す行が多すぎることを示しています。結果を変数に代入する場合は、1 行だけ返す必要があります。

変化する

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 に答える