tskuzzy
tskuzzy

Reputation: 36476

Passing select datename() result to stored procedure

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

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?

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.

Upvotes: 1

Views: 584

Answers (2)

EricZ
EricZ

Reputation: 6205

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

Upvotes: 1

Ta01
Ta01

Reputation: 31630

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

exec my_sp @reportText;

Upvotes: 1

Related Questions