-1

Is there a way to use dynamic date parameter in a stored procedure between CREATE PROCEDURE and BEGIN?

CREATE PROCEDURE dbo.MYSP 
    @StartDate DATETIME = GETDATE(),
    @EndDate DATETIME = GETDATE() - 1
BEGIN
AS

Is this possible?

3
  • Not clear... Wouldn't declaring DATE parameter suffice? Commented Nov 16, 2018 at 20:26
  • BEGIN should start after AS not before and are you sure EndDate is one day before the StartDate? Commented Nov 16, 2018 at 20:31
  • this is the start of my parameter declaration CREATE PROCEDURE dbo.Tableau_Test_SP_Parameter -- Add the parameters for the stored procedure here @StartDate DATETIME=CONVERT(DATE,GETDATE(),101) AS BEGIN but it is giving me a syntax error on CONVERT and GETDATE() Commented Nov 16, 2018 at 21:31

1 Answer 1

2

You can't add GETDATE() default parameter to stored procedure. Instead of this you can do this :

DROP PROCEDURE dbo.MYSP 
GO
CREATE PROCEDURE dbo.MYSP 
    @StartDate DATETIME = NULL,
    @EndDate DATETIME = NULL
AS
BEGIN
IF @StartDate IS NULL SET @StartDate=GETDATE()
IF @EndDate IS NULL SET @EndDate=DATEADD(DAY, -1, GETDATE())

SELECT @StartDate,@EndDate

END
GO
dbo.MYSP 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.