I’m having a problem when converting all table columns with ntext type in my database. I wrote this query in order to alter my columns, but there is a syntax error:
ALTER TABLE mytable ALTER COLUMN mycolumn
VARCHAR(SELECT MAX(DATALENGTH(mycolumn)) FROM mytable);
Even though SELECT MAX(DATALENGTH(mycolumn)) FROM mytable is returning the correct number, the query cannot be executed.
The syntax error is:
Incorrect syntax near the keyword ‘select’.(which is inside the
varchar)
How can I solve the problem?
You will need to execute this as dynamic sql because the size of the column cannot be a variable.
The benefit of this is you could loop over
sys.objectsandsys.columnsto find all ntext columns and convert them to varchar.