Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:48:56+00:00 2026-06-04T12:48:56+00:00

I was tasked with migrating data from a MSSQL server to a MySQL server.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:48:58+00:00Added an answer on June 4, 2026 at 12:48 pm

    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
    

    Demo: http://www.sqlfiddle.com/#!3/5cf87/1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am tasked with migrating a server's networking from plain sockets to SSL in
I'm tasked with exporting data from an old application that is using SQL Anywhere,
im tasked to create a virtual database that clients can create from our web
My team is tasked with migrating a full screen, kiosk-style application for playing back
I've been tasked with encrypting several years worth of sensitive data (don't ask) stored
I've been tasked with reconciling two big data sets (two big lists of transactions).
I have been tasked with taking an Access 97 application and moving the back-end
I have been tasked with migrating an existing mature ASP.NET 2.0 web site to
I have been tasked to replace the hardware an existing Apache/Tomcat/MySQL application is running.
I've been tasked with revisiting a database schema we designed and use internally for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.