将select datename()结果传递给存储过程

时间:2022-08-19 10:12:52

I'm trying to call a stored procedure with a varchar containing the name of the previous month.

我正在尝试使用包含上个月名称的varchar调用存储过程。

exec my_sp @subject='Report for June 2011';

Except June 2011 should be dynamic (dependent upon the month you call the stored procedure), not static. What's the best way to accomplish this?

除2011年6月外,应该是动态的(取决于您调用存储过程的月份),而不是静态的。实现这一目标的最佳方法是什么?

I can get the previous month in the desired format like so:

我可以按照所需的格式获得上个月:

select datename(month, dateadd(month,-1,getdate()))+' '+datename(year, dateadd(month,-1,getdate()))

but I don't know how to pass it to the stored procedure.

但我不知道如何将它传递给存储过程。

2 个解决方案

#1


1  

        Declare @reportText varchar(40);
    Set @reportText =  datename(month, dateadd(month,-1,getdate()))+' '+datename(year, dateadd(month,-1,getdate()))

exec my_sp @reportText;

#2


1  

DECLARE @ReportMonth VARCHAR(100)

SELECT @ReportMonth = 'Report for ' + DATENAME(MONTH, DATEADD(MONTH,-1,GETDATE()))+' '+DATENAME(YEAR, DATEADD(MONTH,-1,GETDATE()))

EXEC my_sp @subject=@ReportMonth

#1


1  

        Declare @reportText varchar(40);
    Set @reportText =  datename(month, dateadd(month,-1,getdate()))+' '+datename(year, dateadd(month,-1,getdate()))

exec my_sp @reportText;

#2


1  

DECLARE @ReportMonth VARCHAR(100)

SELECT @ReportMonth = 'Report for ' + DATENAME(MONTH, DATEADD(MONTH,-1,GETDATE()))+' '+DATENAME(YEAR, DATEADD(MONTH,-1,GETDATE()))

EXEC my_sp @subject=@ReportMonth