I'm using Microsoft SQL Server 2014 Express.
I found myself wanting to add the Columns CompanyNameChangeDate
(1 through 10) and CompanyNameChangePreviousName
(1 through 10) to a table in my database.
I wrote the following query to do it:
declare @i int
set @i = 0
Declare @nameChangeDatePrefix as varchar(255) = 'CompanyNameChangeDate'
Declare @nameChangePreviousNamePrefix as varchar(255) = 'CompanyNameChangePreviousName'
declare @sql as varchar(255)
while (@i < 10)
begin
set @i = @i + 1
set @sql = 'ALTER TABLE company_registry_data ADD ' + @nameChangeDatePrefix + Cast(@i as varchar(255)) + ' DATE'
execute(@sql)
set @sql = 'ALTER TABLE company_registry_data ADD ' + @nameChangePreviousNamePrefix + Cast(@i as varchar(255)) + ' VARCHAR(255)'
execute(@sql)
end
- How could I make it better?
- What naming conventions have I contravened?
- What habits should I start building right now?
- What are the code smells?