I'm using Microsoft SQL server management studio.
I would like to add a new column to a table (altertable1), and name that column using the data from a cell (Date) of another table (stattable1).
DECLARE @Data nvarchar(20)
SELECT @Data = Date
FROM stattable1
WHERE Adat=1
DECLARE @sql nvarchar(1000)
SET @sql = 'ALTER TABLE altertable1 ADD ' + @Data + ' nvarchar(20)'
EXEC (@sql)
Executing this, I get the following error and can't find out why:
"Incorrect syntax near '2021'."
The stattable1 looks like this:
Date |Adat
2021-09-08 |1
2021-09-08 is a daily generated data:
**CONVERT(date,GETDATE())**