Solved

Convert SQL 2008 Query to MySQL 5

Posted on 2009-07-13
12
438 Views
Last Modified: 2012-05-07
I have a SQL server 2008 query that uses the new MERGE statement, and I need to convert it to MySQL 5.  Needless to say, I'm having some problems doing so.  I've looked into the MERGE statement with MySQL, but it seems to be different and not exactly what I'm looking for.  Maybe I'm just not understanding it.  Can anyone help me in converting this or point me in the direction for MySQL 5 equivalent?

Thanks.
MERGE INTO [Movies] As [target]

    USING (SELECT [Dbid], [Status], Studio, Released, Sound, Versions, Price, Rating, [Year], Genre, Aspect, UPC, DVDReleaseDate, [TimeStamp], Name FROM MoviesStaging) AS source

    ON ([target].[Dbid] = source.[Dbid])

    WHEN MATCHED THEN 

        UPDATE SET	Name = source.Name,

			[Dbid] = source.[Dbid],

			[Status] = source.[Status],

			Released = source.Released,

			Sound = source.Sound,

			Versions = source.Versions,

			Price = source.Price,

			Rating = source.Rating,

			[Year] = source.[Year],

			Genre = source.Genre,

			Aspect = source.Aspect,

			UPC = source.UPC,

			DVDReleaseDate = source.DVDReleaseDate,

			[TimeStamp] = source.[TimeStamp]					

	WHEN NOT MATCHED THEN	

	    INSERT ([Dbid], [Status], Studio, Released, Sound, Versions, Price, Rating, [Year], Genre, Aspect, UPC, DVDReleaseDate, [TimeStamp], Name)

	    VALUES (source.[Dbid], source.[Status], source.Studio, source.Released, source.Sound, source.Versions, source.Price, source.Rating, source.[Year], source.Genre, source.Aspect, source.UPC, source.DVDReleaseDate, source.[TimeStamp], source.Name);

Open in new window

0
Comment
Question by:Phreak3eb
  • 4
  • 4
  • 4
12 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24840146
MERGE command is not available in MYSQL. You need to update the existing records and Insert the Missing records.

Logic for your conversion:

1. If a record exists, then update those records into Movies table
2. Else Insert those records into Movies table.
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 250 total points
ID: 24841706
Agree with rrjegan17... basically becomes a two part process :


    UPDATE Movies SET	Name = source.Name,

			[Dbid] = source.[Dbid],

			[Status] = source.[Status],

			Released = source.Released,

			Sound = source.Sound,

			Versions = source.Versions,

			Price = source.Price,

			Rating = source.Rating,

			[Year] = source.[Year],

			Genre = source.Genre,

			Aspect = source.Aspect,

			UPC = source.UPC,

			DVDReleaseDate = source.DVDReleaseDate,

			[TimeStamp] = source.[TimeStamp]					

    From Movies

    inner join MoviesStaging as source on movies.[Dbid] = source.[Dbid]
 
 
 

    INSERT movies ([Dbid], [Status], Studio, Released, Sound, Versions, Price, Rating, [Year], Genre, Aspect, UPC, DVDReleaseDate, [TimeStamp], Name)

    select source.[Dbid], source.[Status], source.Studio, source.Released, source.Sound, source.Versions, source.Price, source.Rating, source.[Year], source.Genre, source.Aspect, source.UPC, source.DVDReleaseDate, source.[TimeStamp], source.Name

    From MoviesStaging as source

    left outer join Movies on movies.[Dbid] = source.[Dbid]

    where movies.[dbid] is NULL

Open in new window

0
 

Author Comment

by:Phreak3eb
ID: 24844839
I'm clearly an SQL noob.  I'm trying to test the UPDATE statement with no luck.  I got the INSERT statement to work though.  Whenever I run the update statement I get:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'From Movies inner join MoviesStaging as source on Movies.Dbid ' at line 16

Here's the slightly modified update statement you gave me.


UPDATE Movies SET

Name = source.Name,

DBID = source.DBID,

Status = source.Status,

ReleaseDate = source.ReleaseDate,

Studio = source.Studio,

Sound = source.Sound,

Versions = source.Versions,

Price = source.Price,

Rating = source.Rating,

Year = source.Year,

Genre = source.Genre,

Aspect = source.Aspect,

UPC = source.UPC,

DVDReleaseDate = source.DVDReleaseDate,

Movies.Timestamp = source.Timestamp

From Movies inner join MoviesStaging as source on Movies.DBID = source.DBID

Open in new window

0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 250 total points
ID: 24845057
The above UPDATE statement is of MS SQL syntax. And you need a MySQL Syntax to make it work.

Hope this helps
UPDATE Movies,MoviesStaging

SET

 Movies.Name = MoviesStaging.Name,

 Movies.DBID = MoviesStaging.DBID,

 Movies.Status = MoviesStaging.Status,

 Movies.ReleaseDate = MoviesStaging.ReleaseDate,

 Movies.Studio = MoviesStaging.Studio,

 Movies.Sound = MoviesStaging.Sound,

 Movies.Versions = MoviesStaging.Versions,

 Movies.Price = MoviesStaging.Price,

 Movies.Rating = MoviesStaging.Rating,

 Movies.Year = MoviesStaging.Year,

 Movies.Genre = MoviesStaging.Genre,

 Movies.Aspect = MoviesStaging.Aspect,

 Movies.UPC = MoviesStaging.UPC,

 Movies.DVDReleaseDate = MoviesStaging.DVDReleaseDate,

 Movies.Timestamp = MoviesStaging.Timestamp

Where Movies.DBID = MoviesStaging.DBID

Open in new window

0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 24848257
Ooops, sorry about that... Well caught rrjegan17.
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24848324
>> Well caught rrjegan17.

It happens..And not a problem
Even I did this mistake earlier and that's why got it this time..

Waiting for Phreak3eb's comment now.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Phreak3eb
ID: 24849332
Thanks guys.  I will try this later today when I'm available and get back to you.
0
 

Author Comment

by:Phreak3eb
ID: 24854523
Ok.  Tried the update and the insert again tonight.  Both "worked", but the insert reported 1 warning, the update reported 18 changes.  Is there a way to get these queries to output that information?  Sorry, I know this question goes a bit off from the original question, but it's kind of part of the overall solution.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 24855211
Depends a little bit From procedures or your code, you can use the mysql_affected_rows() function : http://dev.mysql.com/doc/refman/6.0/en/mysql-affected-rows.html

if running within a procedure or interactively can use the row_count() function immediately following your insert or update statement e.g.

Insert table (columns....)
select columns...
from datasource

select row_count()

And this time i checked to make sure I am in the MySQL frame of mind :)
0
 

Author Comment

by:Phreak3eb
ID: 24892007
Thanks for all the help guys.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 24892758
No problems, and sorry for that little mind lapse earlier - glad rrjegan17 was there to catch....



0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24892814
Welcome..
And thanks Mark too.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now