Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

How to convert stored procedures from MS SQL Server to MySQL

17 September 2020

How to convert stored procedures from MS SQL Server to MySQL

When switching from MS SQL to MySQL, apart from data migration, you must also transfer the application code, which is in the database.

Earlier we discussed how to move MS SQL to a MySQL database using the WorkSQL Workbench tool.

Within the migration, it will only convert tables and copy data, but it will not convert triggers, views, and stored procedures. You must manually convert them to a MySQL database.

To perform this conversion manually, you must understand the basic differences between MS SQL and MySQL queries.

During my conversion from Microsoft SQL Server to a MySQL database, I encountered the following operators and MS SQL queries that were not MySQL compatible, and I had to convert them as shown below.

Creating stored procedures. Syntax

The basic syntax for creating stored procedures is different.

MS SQL Stored, procedure creation syntax:

CREATE PROCEDURE [dbo]. [storedProcedureName]
@someString VarChar(150)
As
BEGIN
-- Sql queries goes here
END

for MySQL procedure creation syntax:

CREATE PROCEDURE storedProcedureName( IN someString VarChar(150) )
BEGIN
-- Sql queries goes here
END

Time table creation

In MS SQL code, I have created several temporary tables that are required to apply. The syntax for creating a temporary table is different, as shown below.

MS SQL syntax for creating a temporary table:

CREATE TABLE #tableName(
emp_id VARCHAR(10)COLLATE Database_Default PRIMARY KEY,
emp_Name VARCHAR(50) COLLATE Database_Default,
emp_Code VARCHAR(30) COLLATE Database_Default,
emp_Department VARCHAR(30) COLLATE Database_Default
)

MySQL syntax for creating a temporary table:

CREATE TEMPORARY TABLE tableName(
emp_id VARCHAR(10),
emp_Name VARCHAR(50),
emp_Code VARCHAR(30),
emp_Department VARCHAR(30)
);

IF syntax

I used many conditions in my stored procedures and triggers that didn’t work after conversion to MySQL because the syntax is different as shown below.

MS SQL condition IF Syntax:

if(@intSomeVal='')
BEGIN
SET @intSomeVal=10
END

MySQL condition IF Syntax:

IF @intSomeVal='' THEN
SET @intSomeVal=10;
END IF;

IF EXIST status

Another common use, if a condition, is to check whether the query returns any lines or not; and if it returns multiple lines, do something. To do this, I used IF EXISTS in MS SQL, which must be converted to MySQL by the IF command as described below.

MS SQL IF EXITS Example:

IF EXISTS(SELECT 1 FROM #tableName WITH(NOLOCK) WHERE ColName='empType' )
BEGIN
-- Sql queries goes here
END

MySQL equivalent is higher, using when the condition is met:

IF(SELECT count(*) FROM tableName WHERE ColName='empType') > 0 THEN
-- Sql queries goes here
END IF;

Date functions

Using data functions within a stored procedure is quite common. The following table shows the differences between MS SQL and MySQL data, related functions.

MS SQL ServerMySQL Server
GETDATE( )NOW( )
SYSDATE( )
CURRENT_TIMESTAMP( )
GETDATE( ) + 1NOW( ) + INTERVAL 1 DAY
CURRENT_TIMESTAMP +INTERVAL 1 DAY
DATEADD(dd, -1, GETDATE())ADDDATE(NOW(), INTERVAL -1 DAY)
CONVERT(VARCHAR(19),GETDATE())DATE_FORMAT(NOW(),’%b %d %Y %h:%i %p’)
CONVERT(VARCHAR(10),GETDATE(),110)DATE_FORMAT(NOW(),’%m-%d-%Y’)
CONVERT(VARCHAR(24),GETDATE(),113)DATE_FORMAT(NOW(),’%d %b %Y %T:%f’)
CONVERT(VARCHAR(11),GETDATE(),6)DATE_FORMAT(NOW(),’%d %b %y’)

 

Announcement of variables

In MS SQL stored procedures, you can declare variables somewhere between “Begin” and “end”.

However, in MySql, you will have to declare them only after you declare the stored “begin” procedure. A declaration of a variable at any point between is not allowed.

Select the first N records

In MS SQL, you will use SELECT, TOP if you want to select only the first few records. For example, to select the 1st 10 records, you will do the following:

SELECT TOP 10 * FROM TABLE;

In MySQL, you will have to use LIMIT instead of TOP as shown below.

SELECT * FROM TABLE LIMIT 10;

Converting an integer number to Char

In MS SQL you will perform the following steps (Convert functions) to convert an integer to a character.

CONVERT(VARCHAR(50), someIntVal)

In MySQL, you will use the CAST function to convert an integer to a character, as shown below.

CAST( someIntVal as CHAR)

Concatenation operator

If you manipulate a lot of data inside a stored procedure, you can use some string concatenation execution.

In MS SQL the concatenation operator + character. An example of such usage is shown below.

SET @someString = '%|' + @someStringVal + '|%'

In MySQL, if you use the AnSi mode, it is the same as in MS SQL. i.e. + character will work for concatenation.

But, in the default MySQL mode, we have to use the CONCAT function (“str1”, “str2”, “str3″… “strN”).

SET someString = CONCAT('%|', someStringVal, '|%');

 
Tags: , , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...