如何从WITH ... SELECT语句声明变量

时间:2017-04-11 09:54:40

标签: sql-server

我在MS SQL SERVER中遇到变量定义问题。你能给我一些提示吗,如何将count函数的结果赋给变量@WorkingDays?

DECLARE @StartDate DATE = '2017-03-29';
DECLARE @EndDate DATE = '2017-05-05';
DECLARE @WorkingDays INT;

    WITH DateRange(DateData) AS 
    (
        SELECT @StartDate as Date
        UNION ALL
        SELECT DATEADD(d,1,DateData)
        FROM DateRange 
        WHERE DateData < @EndDate
    )
    SELECT COUNT(DateData)
    FROM DateRange
    WHERE DATEPART(dw, DateData) not in (1, 7)
    OPTION (MAXRECURSION 0)

我试图使用

set @WorkingDays = (WITH.... OPTION (MAXRECURSION 0) ) 

但它返回错误(语法不正确)

我会感激任何提示

祝你好运

2 个答案:

答案 0 :(得分:4)

SELECT @WorkingDays = COUNT(DateData) ...

答案 1 :(得分:1)

你可以遵循这个方法:

const payload = {
        notification: {
            title: 'SahuSmaj : New Matrimony Post',
            body: `New biodata entered,check it out`,
            sound: "default",
            click_action: "MATRIMONY"
        }
    };