2016-03-30 12 views
0

土曜日または日曜日の場合、最後の作業日として金曜日を使用する必要がある場合は、前の5営業日を解決しようとしています。SQL作業日関数

Iveはこのクエリの他の部分を処理していますが、これを使用してエラーメッセージを受け取る関数を作成しようとすると、iveが逃したアイデアは何ですか?

create table holidays (
    date date); 
GO 



create function dbo.WorkDays 
(

@date datetime, 
@days int 

) 

returns date 

as 

Begin 

IF datename(dw,@date) = 'Saturday' 

select dateadd(dd,-1,thedate) 
    from (
    select thedate=dateadd(d,-v.day,cast(@date as date)), 
     rn=row_number() over (order by v.day), 
     weekday = datename(dw,dateadd(d,-v.day,cast(@date as date))) 
    from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))v(day) 
    left join holidays h on h.date = dateadd(d,v.day,cast(@date as date)) 
    where left(datename(dw,dateadd(d,-v.day,cast(@date as date))),1) <> 'S' 
) x 
    where @days = rn 

Else If datename(dw,@date) = 'Sunday' 

select dateadd(dd,-1,thedate) 
    from (
    select thedate=dateadd(d,-v.day,cast(@date as date)), 
     rn=row_number() over (order by v.day), 
     weekday = datename(dw,dateadd(d,-v.day,cast(@date as date))) 
    from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))v(day) 
    left join holidays h on h.date = dateadd(d,v.day,cast(@date as date)) 
    where left(datename(dw,dateadd(d,-v.day,cast(@date as date))),1) <> 'S' 
) x 
    where @days = rn 

Else 

select thedate 
    from (
    select thedate=dateadd(d,-v.day,cast(@date as date)), 
     rn=row_number() over (order by v.day), 
     weekday = datename(dw,dateadd(d,-v.day,cast(@date as date))) 
    from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))v(day) 
    left join holidays h on h.date = dateadd(d,v.day,cast(@date as date)) 
    where left(datename(dw,dateadd(d,-v.day,cast(@date as date))),1) <> 'S' 
) x 
    where @days = rn 

End 

drop table holidays 
+1

エラーメッセージの内容を教えてください。 – dfundako

答えて

0

エラーを示している:関数内に含ま Select文がクライアントにデータを返すことができません。

create function dbo.WorkDays 
(

@date datetime, 
@days int 

) 

returns date 

as 

Begin 
DECLARE @return date 

IF datename(dw,@date) = 'Saturday' 

select @return = dateadd(dd,-1,thedate) 
    from (
    select thedate=dateadd(d,-v.day,cast(@date as date)), 
     rn=row_number() over (order by v.day), 
     weekday = datename(dw,dateadd(d,-v.day,cast(@date as date))) 
    from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))v(day) 
    left join holidays h on h.date = dateadd(d,v.day,cast(@date as date)) 
    where left(datename(dw,dateadd(d,-v.day,cast(@date as date))),1) <> 'S' 
) x 
    where @days = rn 

Else If datename(dw,@date) = 'Sunday' 

select @return = dateadd(dd,-1,thedate) 
    from (
    select thedate=dateadd(d,-v.day,cast(@date as date)), 
     rn=row_number() over (order by v.day), 
     weekday = datename(dw,dateadd(d,-v.day,cast(@date as date))) 
    from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))v(day) 
    left join holidays h on h.date = dateadd(d,v.day,cast(@date as date)) 
    where left(datename(dw,dateadd(d,-v.day,cast(@date as date))),1) <> 'S' 
) x 
    where @days = rn 

Else 

select @return= thedate 
    from (
    select thedate=dateadd(d,-v.day,cast(@date as date)), 
     rn=row_number() over (order by v.day), 
     weekday = datename(dw,dateadd(d,-v.day,cast(@date as date))) 
    from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10))v(day) 
    left join holidays h on h.date = dateadd(d,v.day,cast(@date as date)) 
    where left(datename(dw,dateadd(d,-v.day,cast(@date as date))),1) <> 'S' 
) x 
    where @days = rn 


    return @return 
End 

私はあなたのクエリのロジックが正しいと仮定します

あなたが@return変数を宣言し、それを各select文の出力を割り当てる必要があります。

+0

すごく感謝して、働いているように見えます:) – user3691566