I have the next query:
DECLARE @startdate DATETIME SET @startdate = ''1/1/1990'' ;WITH cte AS ( SELECT @startdate DateVal UNION ALL SELECT DateVal + 1 FROM cte WHERE DateVal + 1 <= @enddate )
Сan you please explain what it means instruction:
@startdate DateVal
Is a variable called startdate
assigned a different name? But why?
Advertisement
Answer
First, I would write this as:
@startdate as DateVal
The two are equivalent, but this is clearer in intent (and less likely to cause problems).
In any case, @startdate
is not a valid column name in a table — or CTE. So, you want to give it a valid name. That is what this is doing.
If you don’t give it a name, you will get an error, as illustrated here.
You should also use standard date formats, so I would suggest:
SET @startdate = '1990-01-01'; -- ISO standard format but could be affected by internationalization formats
or:
SET @startdate = '19900101'; -- SQL Server standard format. Unambiguous.