Cog Icon signifying link to Admin page

Classic ASP Help

Moving from MySQL to MSSQL

I've not successfully copied a MySQL database over to SQL Server in one go. There is a tool called SQL Server Migration Assistant (https://learn.microsoft.com/en-us/sql/ssma/sql-server-migration-assistant?view=sql-server-ver16) which should allow you to do this, but it requires you to add software to your desktop and to the server with the new database on it, and it's not clear if it works with SQL Server Express which is what I mostly use.

I generally copy the data and structure over table-by-table which will be OK for many simple cases.

Log into MyPHPAdmin and export each table as a CSV. (Don't use CSV for Excel).

Then you'll need to open each CSV with a text editor and remove the blank last line (if it exists).

Once you've done that, open your SQL Server database in SQL Server Management Studio. Right-click the database and select Tasks > Import Flat File.

The wizard will take you through the next step. It analyses the first 50 lines to decide what each column's type is going to be but you can override these. You may well want to turn nvarchar(550) into nvarchar(MAX) for instance it the wizard has assumed a column will have a maximum of 550 characters.

You may need to leave some numeric or date columns as nvarchar() to get them to import, then clean the data up in MSSQL afterwards, and then change the column data types.

SQL Differences

Once you've changed your database and updated your connection string you may well find you have to tweak the SQL code on many of your pages as there are some subtle differences.


Instead of limit SQL Server uses top for example. So

select * from table limit 10

will need to be changed to

select top 10 * from table


MySQL uses backticks to surround dodgy column names. Backticks are the weird apostrophe at the top left of most keyboards.

So for example you might have SQL saying

select * from table order by `date`

which allows the system to know that you mean the column with the name date rather than the function.

If you do have any backticks in your SQL you'll need to change them to square brackets, so

select * from table order by [date]

Some people will have surrounded all the columns in their SQL with backticks - it's only really needed if the column name could be confused with something else or if the name has spaces or hyphens in it.


The rand() function doesn't work the same way, so if you use it you'll find you're not getting random numbers. Replacing it with newid() should work if you don't need truly random numbers and the table isn't ginormous.


Paging is handled differently too.

In MySQL you might have SQL like

select * from table order by id limit 5, 10

This selects 10 rows, starting from row 5.

In SQL server you need to change this to

select * from table order by id offset 5 rows fetch next 10 rows only

^