I needed to create a table with dynamic columns, so I created a cursor that loops through the records of a table and will create the necessary columns, however, is giving me this error:
Incorrect syntax near 'INT'.
Example code:
SELECT @sql = 'ALTER TABLE #temp3 ADD ' + @nome + ' INT' EXEC (@sql); I have also tried this:
EXEC ('ALTER TABLE #temp3 ADD ' + @nome + ' INT') But still the same error
Any suggestions?
Edit: Examples of values that can receive @nome
- Very Bad
- Bad
- Good
- Very Good
@nomecontains, it's anyones guess. As well as/instead ofEXECing the constructed string, tryPRINTing it as well, so you can see what's being run.@nome?exec, you can reference temp tables created in the outer scope. Maybe you're thinking of table variables