[英]Export from Excel to SQL Server with Excel Column of Multiple Values
I am fairly new to database design although I have some SQL skill. 尽管我有一些SQL技能,但我对数据库设计还是比较陌生的。 I have an excel sheet that I wish to upload to SQL Server.
我有一个要上传到SQL Server的Excel工作表。 The issue is I have a column in Excel that has multiple values separated by "/".
问题是我在Excel中有一列,其中有多个用“ /”分隔的值。
For example: 例如:
Fruit Banana/Pear/Orange Pear/Raspberry/... Banana
... ...
I want to split the cell based on "/" which I am fine doing. 我想基于“ /”拆分单元格,这很好。 Then put the values into a table in SQL Server.
然后将值放入SQL Server中的表中。 However, there is no defined amount of "Fruits" that can be in the Excel cell so I need to allow for multiple table fields.
但是,Excel单元格中没有定义的“水果”数量,因此我需要允许多个表字段。
Does anyone have suggestions on how to do this? 有人对此有建议吗? I wrote an ADODB connection to export from Excel to SQL Server but don't know how handle this cell.
我编写了一个ADODB连接,以便从Excel导出到SQL Server,但是不知道如何处理此单元格。
Thank you 谢谢
Another option is to split your data after it has been loaded. 另一种选择是在加载数据后拆分数据。 For example
例如
Declare @YourTable table (ID int,Fruit varchar(150))
Insert into @YourTable values
(1,'Banana/Pear/Orange'),
(2,'Pear/Raspberry/Apple'),
(3,'Banana')
Select A.ID
,B.Key_Value
From @YourTable A
Cross Apply (Select * from [dbo].[udf-Str-Parse](A.Fruit,'/')) B
Returns 退货
ID Key_Value
1 Banana
1 Pear
1 Orange
2 Pear
2 Raspberry
2 Apple
3 Banana
The UDF UDF
CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimeter varchar(10))
--Usage: Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
-- Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
Returns @ReturnTable Table (Key_PS int IDENTITY(1,1), Key_Value varchar(max))
As
Begin
Declare @XML xml;Set @XML = Cast('<x>' + Replace(@String,@Delimeter,'</x><x>')+'</x>' as XML)
Insert Into @ReturnTable Select ltrim(rtrim(String.value('.', 'varchar(max)'))) FROM @XML.nodes('x') as T(String)
Return
End
Use a Bridge Table . 使用桥表 。 This way each row in Fruit table can have N types.
这样,水果表中的每一行都可以具有N个类型。
Description values in table Types will be Banana, Pear, Orange, Raspberry, etc. 表类型中的描述值将为香蕉,梨,橙,树莓等。
When you design the table it will have to have a defined number of columns. 设计表时,它必须具有定义的列数。
The simplest approach would be to assign an ID for each group. 最简单的方法是为每个组分配一个ID。
ID Fruit
1 Banana
1 Pear
1 Orange
2 Pear
2 Raspberry
3 Banana
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.