SQL Server: Insert Multiple Rows to a table based on a column in a different table

Melissa Balle

I have a table

CREATE TABLE [StudentsByKindergarten]
(
    [FK_KindergartenId] [int] IDENTITY(1,1) NOT NULL,
    [StudentList] [nvarchar]
)

where the entries are

(1, "John, Alex, Sarah")
(2, "")
(3, "Jonny")
(4, "John, Alex")

I want to migrate this information to the following table.

CREATE TABLE [KindergartenStudents]
(
    [FK_KindergartenId] [int] NOT NULL,
    [StudentName] [nvarchar] NOT NULL)
)

so that it will have

(1, "John")
(1, "Alex")
(1, "Sarah")
(3, "Jonny")
(4, "John")
(4, "Alex")

I think I can achieve split function using something like the answer here: How do I split a string so I can access item x?

Using the function here:

http://www.codeproject.com/Articles/7938/SQL-User-Defined-Function-to-Parse-a-Delimited-Str

I can do something like this,

INSERT INTO [KindergartenStudents] ([FK_KindergartenId], [Studentname])
    SELECT 
        sbk.FK_KindergartenId,
        parsed.txt_value
    FROM 
        [StudentsByKindergarten] sbk, dbo.fn_ParseText2Table(sbk.StudentList,',') parsed
GO

but doesn't seem to work.

Zanon

Based on this question, I've learned a better approach for this problem. You just need to use CROSS APPLY with your suggested function fn_ParseText2Table.

Sample Fiddle

INSERT INTO KindergartenStudents
  (FK_KindergartenId, StudentName)
SELECT
  sbk.FK_KindergartenId,
  parsed.txt_value
FROM
  StudentsByKindergarten sbk
    CROSS APPLY
  fn_ParseText2Table(sbk.StudentList, ',') parsed

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

来自分类Dev

Microsoft SQL server: INSERT to table with 's

来自分类Dev

Check duplicates in nvarchar column of table in SQL Server

来自分类Dev

SQL Server matching all rows from Table1 with all rows from Table2

来自分类Dev

SQL Server: Can Merge Insert Conditionally or Update a Source Table?

来自分类Dev

Select rows with dynamic names from different table as column name in new table matched by articleId

来自分类Dev

combine multiple rows value in mysql table

来自分类Dev

Mapping a column of a table to different class using Rails

来自分类Dev

Using date from one table in a where clause with a column in a different table

来自分类Dev

SQL Table Rows to Columns - Possible PIVOT?

来自分类Dev

Excel or SQL Server - Multiple columns to rows

来自分类Dev

Insert trigger ends up inserting duplicate rows in partitioned table

来自分类Dev

Copying over part of a table from SQL Server to Aurora DB (Based on MySQL by AWS)

来自分类Dev

SQL Query to get the related data in different table

来自分类Dev

XPath read values from sql table column

来自分类Dev

MySQL Grouping similar rows based on entries in a second table

来自分类Dev

Insert values from A table to A table

来自分类Dev

Cannot insert the value NULL into column 'Discriminator', table '...AspNetUsers'; column does not allow nulls. INSERT fails

来自分类Dev

SQL SERVER PIVOT TABLE一行

来自分类Dev

Is it possible to pass the sum of a column from an HTML table on a DIFFERENT website to a db?

来自分类Dev

CREATE TABLE..INSERT ALL INTO 字符错误 - Oracle SQL

来自分类Dev

SQL query should not return rows that contain null values in pivot table

来自分类Dev

SQL 视图(id + count_table1_column1 + count_table2_column_1)

来自分类Dev

Moving records to another table vs. "flag" column SQL performance

来自分类Dev

Deactivate hover state on table rows in Bootstrap Table

来自分类Dev

PL/SQL Inserting into a table minus another table but inserting another column at the same time

来自分类Dev

SQL Server中的CREATE TABLE语句不受ROLLBACK的影响吗?

来自分类Dev

SQL Server - SELECT all values in table without having them in the GROUP BY

来自分类Dev

涉及CREATE TABLE的Microsoft SQL Server语法错误

Related 相关文章

  1. 1

    How to insert multiple rows from a table to another table based on date condition (PHP-MySQL-Query)?

  2. 2

    Microsoft SQL server: INSERT to table with 's

  3. 3

    Check duplicates in nvarchar column of table in SQL Server

  4. 4

    SQL Server matching all rows from Table1 with all rows from Table2

  5. 5

    SQL Server: Can Merge Insert Conditionally or Update a Source Table?

  6. 6

    Select rows with dynamic names from different table as column name in new table matched by articleId

  7. 7

    combine multiple rows value in mysql table

  8. 8

    Mapping a column of a table to different class using Rails

  9. 9

    Using date from one table in a where clause with a column in a different table

  10. 10

    SQL Table Rows to Columns - Possible PIVOT?

  11. 11

    Excel or SQL Server - Multiple columns to rows

  12. 12

    Insert trigger ends up inserting duplicate rows in partitioned table

  13. 13

    Copying over part of a table from SQL Server to Aurora DB (Based on MySQL by AWS)

  14. 14

    SQL Query to get the related data in different table

  15. 15

    XPath read values from sql table column

  16. 16

    MySQL Grouping similar rows based on entries in a second table

  17. 17

    Insert values from A table to A table

  18. 18

    Cannot insert the value NULL into column 'Discriminator', table '...AspNetUsers'; column does not allow nulls. INSERT fails

  19. 19

    SQL SERVER PIVOT TABLE一行

  20. 20

    Is it possible to pass the sum of a column from an HTML table on a DIFFERENT website to a db?

  21. 21

    CREATE TABLE..INSERT ALL INTO 字符错误 - Oracle SQL

  22. 22

    SQL query should not return rows that contain null values in pivot table

  23. 23

    SQL 视图(id + count_table1_column1 + count_table2_column_1)

  24. 24

    Moving records to another table vs. "flag" column SQL performance

  25. 25

    Deactivate hover state on table rows in Bootstrap Table

  26. 26

    PL/SQL Inserting into a table minus another table but inserting another column at the same time

  27. 27

    SQL Server中的CREATE TABLE语句不受ROLLBACK的影响吗?

  28. 28

    SQL Server - SELECT all values in table without having them in the GROUP BY

  29. 29

    涉及CREATE TABLE的Microsoft SQL Server语法错误

热门标签

归档