如何创建 .fmt 文件,并将数据从混合输入文件导出到多个表中
我正在尝试使用 bcp 实用程序设置 .fmt 文件。我之前已经成功地创建了这个(使用数据导出到的表作为输入):
bcp cmsDatabase.dbo.Table1 format nul -T -c -f Table1Format.fmt
9.0
3
1 SQLCHAR 0 12 "\t" 1 Col1_Table1 ""
2 SQLCHAR 0 100 "\t" 2 Col2_Table1 SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 0 "\t" 3 Col3_Table1 SQL_Latin1_General_CP1_CI_AS
Table1 从中创建它: Col1_Table1 int(主键) Col2_Table1 nvarchar(50) Col3_Table1 nvar
现在我面临一个问题。我有输入 .txt/.csv 文件(分隔平面文件),其中的数据不会直接导入到一个表中,它必须进入许多表(并且,一个直接进入行,其他数据需要输入到表 2 的列),示例输入文件的数据:
"Col1_Table1x" "Col2_Table1x" "Col3_Table1x" "Col1_Table2x, Col1_Table2y, Col1_Table2z"
更新: 在上面的文件中,值必须进入:
Col1_Tablex -> Col1_Table1 column of Table1
Col2_Tablex -> Col2_Table1 column of Table1
Col3_Table1x -> Col3_Table1 column of Table1
Table1:
Col1_Table1 Col2_Table1 Col3_Table1
-------------------------------------------
Col1_Table1x Col2_Table1x Col3_Table1x
到这里,我就可以让它工作了。
我想弄清楚:
“Col1_Table2,Col1_Table2,Col1_Table2,.....” ->要放置在 Table2 中的多条记录,用逗号分隔的值填充 Col_Table2,以及 以 Col1_Table1 作为外键。
即 Table2 应该具有
Col1_Table1 Col1_Table2
----------------------------------------------
Col1_Table1x Col1_Table2x
Col1_Table1x Col1_Table2y
Col1_Table1x Col1_Table2z
Col1_Table2 需要作为行进入 Table2 的位置(并且以逗号分隔),并且 Col1_Table1 是 Table2 的外键,因此也需要复制它。
有没有办法创建一个 .fmt 文件来允许复制这种混合输入?
其他详细信息:每次我需要从此输入文件加载表时,我可以截断所有旧数据并重新填充。任何列都可以有特殊字符,如 <、" 、& 等,那么有没有办法处理这个问题呢?
Am trying to set up a .fmt file using the bcp utility. I have been successfully able to create this before thus (using the table that data gets exported to as input):
bcp cmsDatabase.dbo.Table1 format nul -T -c -f Table1Format.fmt
9.0
3
1 SQLCHAR 0 12 "\t" 1 Col1_Table1 ""
2 SQLCHAR 0 100 "\t" 2 Col2_Table1 SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 0 "\t" 3 Col3_Table1 SQL_Latin1_General_CP1_CI_AS
Table1 from which it was created:
Col1_Table1 int (primary key)
Col2_Table1 nvarchar(50)
Col3_Table1 nvar
Now I'm facing an issue. I have the input .txt/.csv file (delimited flat file) with data that will not be imported directly into one table, it has to go into many tables (AND, one directly goes into rows, other data needs to be input into columns of table2), Example Input File's data:
"Col1_Table1x" "Col2_Table1x" "Col3_Table1x" "Col1_Table2x, Col1_Table2y, Col1_Table2z"
Update:
In the above file, values have to go into:
Col1_Tablex -> Col1_Table1 column of Table1
Col2_Tablex -> Col2_Table1 column of Table1
Col3_Table1x -> Col3_Table1 column of Table1
Table1:
Col1_Table1 Col2_Table1 Col3_Table1
-------------------------------------------
Col1_Table1x Col2_Table1x Col3_Table1x
Till here, I can get it to work.
What I'm trying to figure out:
"Col1_Table2, Col1_Table2, Col1_Table2, ....." -> Multiple records to be placed in Table2, populating Col_Table2 with the comma-seperated values, and
with Col1_Table1 as foreign key.
i.e Table2 should have
Col1_Table1 Col1_Table2
----------------------------------------------
Col1_Table1x Col1_Table2x
Col1_Table1x Col1_Table2y
Col1_Table1x Col1_Table2z
where Col1_Table2 needs to go as rows into Table2 (and is comma seperated), and Col1_Table1 is a foreign key for Table2 so it needs to be copied over too.
Is there a way to create a .fmt file that will allow this kind of mixed-input to be copied over?
Additional details: Each time I need to load up the tables from this input file, I can truncate all old data and re-populate. Any of the columns can have special characters like <, " , & etc. so is there a way to handle that too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果数据量较小/中等,那么您可以简单地将数据导入到临时表中,然后在不同的步骤中导入到两个表中。
此外,您还可以通过使用两个不同的格式文件并使用它们插入到不同的表中来跳过数据文件中的几列,从而跳过其他表中的列。
http://msdn.microsoft.com/en-us/library/ms187908.aspx
对于特殊字符,解决此问题的唯一好方法是使用多个列分隔符。我从事音乐行业,因此有时我会使用 3 个不同的列分隔符来正确导入数据。
If the volume of data is small/moderate then can you simply import the data into a staging table and import to the two tables in a different step.
Also you could skip few columns in the data file by using two different format files and using them to insert into different tables skipping the columns from other tables.
http://msdn.microsoft.com/en-us/library/ms187908.aspx
Coming to special characters, the only good way to solve this is use multiple column delimiters. I am in the music industry, so sometimes I use 3 different column delimiters to import the data correctly.