Search code examples
sqlsql-server-2017

Executing stored procedure with date attached


Everyday I have to run code that executes a stored procedure. I hard code yesterday's date after it.

EXECUTE sprm_example '2021-03-10'

I tried to do:

EXECUTE sprm_example 'getdate()-1' EXECUTE sprm_example 'cast(getdate()-1 as date)'

Neither of these work, and it also doesn't work without quotes around it. Any ideas?


Solution

  • SQL Server does not allow expressions in procedure calls but you can use a variable. So:

    declare @date date;
    
    select @date = dateadd(day, -1, getdate());
    
    EXECUTE sprm_example @date;