[英]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? 我如何编写一次一次通过ID2-ID100列并从列和Name列中获取值并向表中添加新行的语句?
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. 我已经做到了,但是确实想写100条选择线。
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.
RDBMS并非旨在干净地处理此设计。 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. 如果基础表在这些字段上
NOT NULL
约束,则WHERE XXXX IS NOT NULL
,但以我的经验来看,它们不是。 You could also leave it out if you know you really need explicit NULL
values, but that's generally considered poor design. 如果您知道确实需要显式的
NULL
值,也可以将其省略,但这通常被认为是较差的设计。 NULL
is too ambiguous. NULL
太含糊。
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. 虽然,我认为您需要在
MyTable
某个位置具有Field_ID
才能使这一切有意义。
如果您使用外键概念,则可以使用excel来写那些选择
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.