I was tasked with migrating data from a MSSQL server to a MySQL server. I figured it’d be easier for me to first convert the MSSQL database to an MS Access database, so I could mess with it, then export it as MySQL.
So, I have a table, called companies:
companyID | name | c_phon | c_email | c_address
1 StackOverflow 5555555555 Joel@stackoverflow.com NYC
2 Google 5558675309 Google@google.com NYC
In our new database, we’ve changed how these fields are being saved. Instead of each row having a phone number, email, and address, we’re using a prefs table.
prefs:
prefID | prefName
1 c_phone
2 c_email
3 c_address
My issue is, how to I convert each field into it’s own row in our companyPrefs table. It should look something like this:
companyID | prefID | prefValue
1 1 5555555555
1 2 Joel@stackoverflow.com
1 3 NYC
2 1 5558675309
2 2 Google@google.com
2 3 NYC
I don’t know how to use vbscript or whatever, so I was trying to do this using SQL.
INSERT INTO comanyPrefs (companyID, prefID, prefValue)
SELECT companyID, prefID, @fieldName
FROM companies, preferences
WHERE @fieldName = prefName
Obviously this doesn’t work. How can I select fields using a variable field name? How can I insert a row for each field?
You’re on the right track. However, you would need to insert for each possible field, something like this:
Demo: http://www.sqlfiddle.com/#!3/5cf87/1