There are some difference in SQL syntax for MySQL and MS SQL. One of the differences is TOP and LIMIT keywords. And this is bad. Because LIMIT comes in the end of query and TOP comes in the beginning. So, simply to replace is not good. I'm missing the ability of LIMIT to use 2 parameters... but this is possible too, and you need to remember this and add this support if you find this code useful.
[code=c#;Simply approach to make MySQL queries work on MS SQL]/// <summary>
/// Converts MySQL "LIMIT" queries to MS SQL "TOP" queries
/// </summary>
/// <param name="q">Query</param>
/// <returns></returns>
public string MakeQuerySQLServerCompatible(string q)
{
string res = q;
Regex limit = new Regex("SELECT(.+)LIMIT (\\d+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = limit.Match(q);
if (m.Groups.Count == 3)
{
string beforeLimit = m.Groups[1].Value;
string limitCount = m.Groups[2].Value;
res = string.Format("SELECT TOP ({0}) {1}", limitCount, beforeLimit);
}
return res;
}[/code]
This stuff is tricky, that's why I decided to post it. The idea to remove LIMIT stuff and replace it with SELECT TOP statement. These queries special abilities always on high demand because almost every query should define the max amount of records to receive.
Any suggestions how to support "LIMIT 10,20"-like queries in MS SQL ?
Still overloaded with work, maybe add more later.