Asignar campos a su propia fila en MS Access

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      NYC
    2       Google            5558675309           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
    1         3      NYC
    2         1      5558675309
    2         2
    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

1 Respuestas

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
SELECT companyID, (select prefID from prefs where prefName = 'c_email'), c_email
FROM companies where c_email is not null
SELECT companyID, (select prefID from prefs where prefName = 'c_address'), c_address
FROM companies where c_address is not null


contestado el 22 de mayo de 12 a las 22:05

I was hoping I wouldn't have to manually add each field name to the query. I was hoping I could do this dynamically. - Cohete Hazmat

Well you could construct the code dynamically from VBA. For example, build the query in a string, and for each field name, e.g. strQuery = strQuery & "SELECT companyID ... where prefName = '" & theFieldName & "', " & theFieldName & " FROM companies where " & theFieldName & " is not null", for example. You would also have to do possible casting if any of your source columns are not textual. - mellamokb

I don't know how to use VB, I think it would take me longer to do that than just manually entering the field names :-P - Cohete Hazmat

And if you wanted to do it entirely in SQL, you could build the string the same way using a varchar variable, concatenating the query, and executing the result with exec(@sql). - mellamokb

Ya, if you don't have a whole bunch of fields, say 15-20, and this is a one-time process, or the code only has to be written once, might be just as easy to manually key it all out. I'm a fan of cost-value based decision over cleverness :) - mellamokb

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.