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.
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?
preguntado el 22 de mayo de 12 a las 21:05
You're on the right track. However, you would need to insert for each possible field, something like this:
INSERT INTO companyPrefs (companyID, prefID, prefValue) SELECT companyID, (select prefID from prefs where prefName = 'c_phone'), c_phone FROM companies where c_phone is not null UNION SELECT companyID, (select prefID from prefs where prefName = 'c_email'), c_email FROM companies where c_email is not null UNION SELECT companyID, (select prefID from prefs where prefName = 'c_address'), c_address FROM companies where c_address is not null