1
Declare @DaysLeft as INT


;With MyCTE AS
(
    SELECT number + 1 as 'CurrentDay'
    FROM   master..spt_values
    WHERE  type='p'
    AND    number < datepart(dd, DateAdd(day,-1,DateAdd(Month,1,DateAdd(Month,
                                          DateDiff(Month, 0, GETDATE()),0))))
    AND    datename(WEEKDAY,DateAdd(Month, DateDiff(Month, 0, GETDATE()),
                                         number) ) not in ('Saturday','Sunday')
)

Select @DaysLeft = (Select COUNT(*) As MyTotal from MyCTE
WHERE CurrentDay >= DATEPART(DD, GETDATE()))

Select @DaysLeft
4

1 に答える 1

2

ビューで変数を宣言することはできません。変数を削除し、最後の選択ステートメントを言い換えます。

With MyCTE AS
(
    SELECT number + 1 as 'CurrentDay'
    FROM   .spt_values
    WHERE  type='p'
    AND    number < datepart(dd, DateAdd(day,-1,DateAdd(Month,1,DateAdd(Month,
                                          DateDiff(Month, 0, GETDATE()),0))))
    AND    datename(WEEKDAY,DateAdd(Month, DateDiff(Month, 0, GETDATE()),
                                         number) ) not in ('Saturday','Sunday')
)

Select COUNT(*) As DaysLeft from MyCTE
WHERE CurrentDay >= DATEPART(DD, GETDATE())

SQL Fiddleで変更を実行します。

于 2013-02-22T05:49:20.123 に答える