如何使用 CSV 数据更新 SQL 表?
我正在尝试使用源 CSV 文件中的新列更新 SQL 表之一。此文件中的这些 CSV 记录已在我的 SQL 表中,但缺少此 CSV 文件中的某些列。
我已经将新列添加到我的 SQL 表中。但现在我只需要从新列导入数据。我怎样才能做到最好?
我正在尝试使用 SSIS 2008 来执行此操作。我创建了一个平面文件 CSV 数据源。然后我添加了一个 OLE DB 命令。我将 SQL 命令设置如下:
update berkeley.dbo.j5c_ALL_DATA
set disabilitycode=[Stu Pri Dis],
IEPID=SpecED
,[MT_AR_CR]=[Met At-Rsk Crit]
,[ECP]=[Early Childhood Program]
,[REF_CFC]=[Ref by CFC]
,[IEP_COM_DT]=[IEP Cmp Date]
,[E1_NUM]=[EI Nbr]
,[RDIT]=[Reason for Delay in Transition]
,[MT_PFA_HI]=[Met PFA Inc Crt]
,[FS]=[Family Structure]
,[Hm_Sch_RCDTS]=[RCDTS Home]
,[Srv-Sch_RCDTS]=[RCDTS Serve]
from berkeley.dbo.j5c_ALL_DATA A join Berkeley_CSV2 b ON
A.studentid = B.[SAP ID] and A.BegEnrollDate = B.[Enroll Date]
其中 Berkeley_CSV2 是我的 CSV 平面文件连接管理器的名称。但在此我收到一个错误:
The connection manager "Berkeley_CSV2" is an incorrect type. The type required is "OLEDB". The type available to the component is "FLATFILE".
你可以向我推荐添加此数据的替代方法或不同的 SQL 命令吗?
I am trying to update one of my SQL tables with new columns in my source CSV file. These CSV records in this file are already in my SQL table, but they are lacking some of the columns from this CSV file.
I already added the new columns to my SQL table. But now I just need to import the data from the new columns. How can I do this best?
I am trying to use SSIS 2008 to do this. I created a Flat File CSV Data Source. Then I added an OLE DB command. And I set the SQL command as follows:
update berkeley.dbo.j5c_ALL_DATA
set disabilitycode=[Stu Pri Dis],
IEPID=SpecED
,[MT_AR_CR]=[Met At-Rsk Crit]
,[ECP]=[Early Childhood Program]
,[REF_CFC]=[Ref by CFC]
,[IEP_COM_DT]=[IEP Cmp Date]
,[E1_NUM]=[EI Nbr]
,[RDIT]=[Reason for Delay in Transition]
,[MT_PFA_HI]=[Met PFA Inc Crt]
,[FS]=[Family Structure]
,[Hm_Sch_RCDTS]=[RCDTS Home]
,[Srv-Sch_RCDTS]=[RCDTS Serve]
from berkeley.dbo.j5c_ALL_DATA A join Berkeley_CSV2 b ON
A.studentid = B.[SAP ID] and A.BegEnrollDate = B.[Enroll Date]
where Berkeley_CSV2 is the name of my CSV flat file connection manager. But on this I get an error:
The connection manager "Berkeley_CSV2" is an incorrect type. The type required is "OLEDB". The type available to the component is "FLATFILE".
Can u either recommend to me an alternative way of adding this data or else a different SQL command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
处理这个问题的最佳方法是分多个步骤。不要放弃当前的工作;它将作为不同解决方案的一部分。
首先,在数据库中创建一个临时表。这将是平面文件中数据的中间临时位置。为了简化后续步骤,最好为平面文件中的每个字段创建一列,即使不需要某些数据。在返回 SSIS 设计之前创建表。
在 SSIS 中,创建一个到数据库的 OLE DB 连接管理器和另一个到平面文件的连接管理器。鉴于您问题中的信息,您可能已经准备就绪。
创建数据流任务。在数据流任务中,创建平面文件源并将其链接到您的平面文件。创建 OLE DB 目标并将其链接到临时表。映射从源到目标的数据流路径。
下一步是调整 OLE DB 命令以处理新表中的数据。在“控制流”工作区中,创建从新数据流任务到现有 OLE DB 命令的优先约束(两个任务之间的行)。这将使数据流首先运行,然后是 OLE DB 命令。
修改 OLE DB 命令中的 SQL 脚本,以便它引用新表和关联的列名称。该脚本不应引用该文件。我认为 SQL 脚本中不需要进行任何其他更改。
The best way to handle this is in multiple steps. Don't throw away your current work; it will fit in as part of a different solution.
First, create a staging table in your database. This will be a intermediate, and temporary, location for the data from the flat file. To simplify subsequent steps, it may be best to create a column for each field in the flat file, even though some of the data will not be needed. Create the table before you get back to the SSIS design.
In SSIS, create a OLE DB connection manager to your database and another to your flat file. You probably already have this in place, given the information in your question.
Create a Data Flow task. In the Data Flow task, create a Flat File Source and link it to your flat file. Create an OLE DB Destination and link it to your staging table. Map a data flow path from the Source to the Destination.
The next step is to adapt your OLE DB Command to work with data in the new table. In the Control Flow workspace create a Precedence Constraint (line between two tasks) from the new Data Flow task to your existing OLE DB Command. This will make the Data Flow run first, followed by the OLE DB Command.
Modify your SQL script in the OLE DB Command, so that it references the new table and associated column names. The script should not reference the file. I don't think there are any other changes needed in the SQL script.