2
My goal is to create a SQL Server, which has by default two variables, one with the initial date of (hoje - 3 anos)
and a final date equal to hoje
. I already use for other operations (for example a between) this format to catch the date.
I tried to do it this way:
Create procedure EXTRAÇÃO_SD1_SD2
-- Receber data formato 'AAAMMDD'
@dataini VARCHAR(MAX) = REPLACE(CONVERT(char(10), GetDate()-1080,126),'-','')
@datafim as varchar(10) = REPLACE(CONVERT(char(10), GetDate(),126),'-','')
As
Begin
-- continua procedure
But I get the error below when running
Msg 102, Level 15, Status 1, Procedure EXTRAIO_SD1_SD2, Line 5 [Batch Start Line 0] Incorrect syntax near '('.
What’s wrong with it?
Thanks for the answer, I did it that way because as I said I would like those variables to have a default value when no parameter existed in the Procedure call. I followed the example given on this site (https://technet.microsoft.com/pt-br/library/ms189330(v=sql.105).aspx). I will take a test with your example
– Bruno Dantas
Simple value assignments are allowed, what you cannot do is use functions where variables are declared.
– Ismael
Okay, in this case I understand you have no solution to the submitted case right? That is if I change the way you specified the variables '@dataini' and '@datafim' will always have the value : REPLACE(CONVERT(char(10), Getdate()-1080,126),'-','') and REPLACE(CONVERT(char(10), Getdate(),126),'-',') respectively, correct @Ismael ?
– Bruno Dantas
That’s right Bruno. I will update the response and include the parameters and allow a dynamic value to these variables.
– Ismael