这是我的程序:
create procedure sj_busdays_prevmonth @dtStartDate datetime, @dtEnddate datetime--, @busdays_month int output
as
return
SELECT Cast(
(DATEDIFF(dd, @dtStartDate, @dtEndDate) + 1)
-(DATEDIFF(wk, @dtStartDate, @dtEndDate) * 2)
-(CASE WHEN DATENAME(dw, @dtStartDate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @dtEndDate) = 'Saturday' THEN 1 ELSE 0 END)as int)
以下是我的发言:
declare @nBusDays int
declare @dtStartDate datetime
declare @dtEndDate datetime
SET @dtStartDate = cast(CONVERT(VARCHAR(10),DATEADD(m,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0)),111) as datetime)
SET @dtEndDate = cast(CONVERT(VARCHAR(10),DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)),111) as datetime)
exec @nBusDays = sj_busdays_prevmonth @dtStartDate,@dtEndDate
print @nBusDays
这是我的结果:
0
它应该是23
,这里发生了什么?
答案 0 :(得分:0)
更改
return
SELECT Cast(...
要
RETURN Cast(...
因为您需要返回CAST
。
(您也可以RETURN (SELECT ...)
)
答案 1 :(得分:0)
您需要更改存储过程代码 -
create procedure sj_busdays_prevmonth @dtStartDate datetime, @dtEnddate datetime--,@busdays_month int output
as
SELECT Cast(
(DATEDIFF(dd, @dtStartDate, @dtEndDate) + 1)
-(DATEDIFF(wk, @dtStartDate, @dtEndDate) * 2)
-(CASE WHEN DATENAME(dw, @dtStartDate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @dtEndDate) = 'Saturday' THEN 1 ELSE 0 END)as int)
然后运行您的语句以获得所需的结果。 或者您也可以像这样更改代码 -
create procedure sj_busdays_prevmonth @dtStartDate datetime, @dtEnddate datetime,@busdays_month int output
as
SELECT @busdays_month = Cast(
(DATEDIFF(dd, @dtStartDate, @dtEndDate) + 1)
-(DATEDIFF(wk, @dtStartDate, @dtEndDate) * 2)
-(CASE WHEN DATENAME(dw, @dtStartDate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, @dtEndDate) = 'Saturday' THEN 1 ELSE 0 END)as int)
然后调用你的陈述 -
declare @nBusDays int
declare @dtStartDate datetime
declare @dtEndDate datetime
SET @dtStartDate = cast(CONVERT(VARCHAR(10),DATEADD(m,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0)),111) as datetime)
SET @dtEndDate = cast(CONVERT(VARCHAR(10),DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)),111) as datetime)
exec sj_busdays_prevmonth @dtStartDate,@dtEndDate,@nBusDays OUT
print @nBusDays