I am trying to write a migration script to take one column of comma separated values and insert them individually into a new table in SQL server.
The current column is a list of email addresses and I am creating a new table to hold the collection of email addresses for each company.
I would assume I need to first split the string and then run through and INSERT statement for each entry in the array.
Maybe something like this...?
SELECT Id, value
FROM Companies
CROSS APPLY STRING_SPLIT(Email, ',')
INSERT INTO emailAddresses(emailAddress, companyId)
VALUES (value, Id)
But I don't know how to keep the INSERT statement running for all values.
1条答案
按热度按时间ymdaylpp1#
Use
insert...select
.First, create and populate sample table (Please save us this step in your future questions):
Then, the insert statement:
A quick select to see the results:
Results:
See a live demo on db<>fiddle