简体   繁体   中英

How to insert rows using values from mulitple columns in the same table where there are over 100 columns? Possible use LOOP?

enter code here

Lets say i have the following table:

ID Name    ID2 ID3 ID4 ID100
1   Smith   2   3   4   100
20  Joe     10  20  30  200
30  Jane    40  50  60  300

How can i write a statement that goes thru columns ID2 - ID100 one at a time and take the values from the column and the Name column and adds a new row to the table?

To look like so:

ID Name    ID2 ID3 ID4 ID100
1   Smith   2   3   4   100
2   Smith               
3   Smith               
4   Smith               
100 Smith

Ive done this, but done want to write 100 select lines.

INSERT INTO mytable
(ID, Name)
SELECT ID2, Name FROM mytable WHERE Name = 'Smith'
UNION ALL
SELECT ID3, Name FROM mytable WHERE Name = 'Smith'
UNION ALL
SELECT ID4, Name FROM mytable WHERE Name = 'Smith';

Your database violates First Normal Form by creating repeating groups. An RDBMS is not designed to handle this design cleanly. This is precisely why you don't want to create repeating groups. Any solution other than redesign will be ugly and slow.

The answer, if redesign isn't an option, is to do the painful part once and then not need do it again.

Create the following view:

CREATE VIEW MyUglyTable_Unpivot (ID, Name, Field_ID, Field_Value) AS
SELECT ID, Name, 'ID2',   ID2   FROM MyTable WHERE ID2   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID3',   ID3   FROM MyTable WHERE ID3   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID4',   ID4   FROM MyTable WHERE ID4   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID5',   ID5   FROM MyTable WHERE ID5   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID6',   ID6   FROM MyTable WHERE ID6   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID7',   ID7   FROM MyTable WHERE ID7   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID8',   ID8   FROM MyTable WHERE ID8   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID9',   ID9   FROM MyTable WHERE ID9   IS NOT NULL UNION ALL
...
SELECT ID, Name, 'ID99',  ID99  FROM MyTable WHERE ID99  IS NOT NULL UNION ALL
SELECT ID, Name, 'ID100', ID100 FROM MyTable WHERE ID100 IS NOT NULL

Obviously, you can pick more sensible names for the view and the fields. You can leave out the WHERE XXXX IS NOT NULL if the underlying table has NOT NULL constraints on those fields, but in my experience they don't. You could also leave it out if you know you really need explicit NULL values, but that's generally considered poor design. NULL is too ambiguous.

The view will be slow as hell, but it will let you do operations like this more easily. If you're able to redesign your system to not use repeating groups, I strongly encourage you to do so.

Now you can do this:

INSERT INTO MyTable (ID, Name)
SELECT Field_Value, Name FROM MyUglyTable_Unpivot
WHERE Name = 'Smith'

Although, I'm thinking you need to have Field_ID in MyTable somewhere to make sense of it all.

如果您使用外键概念,则可以使用excel来写那些选择

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM