I have a SQL Server table which has 625 columns created with different datatypes like int, varchar(25), decimal(18, 2), etc…
Now am interested in changing datatype of all columns to varchar(255). Instead of executing below query for all the columns one by one, is there a single SQL Server query to change datatypes of all columns in a table at one shot?
ALTER TABLE dbo.Employee
ALTER COLUMN FirstName VARCHAR(255) NOT NULL
Looking forward for your response.
There is no one single “magic” bullet to do this – it’s an operation that’s rather unusual, so it’s not supported natively.
What you can do is iterate over the columns of your table from the system catalog views, create such an ALTER statement on the fly, and also execute it – something like this:
Replace the
YourTableNameHerewith your actual table name – and you should be good to go! Test this on a copy of your live data first !