如果文件夹中存在多个表,则将多个表从CSV导入到SQL [英] Import multiple tables into SQL from CSV if existing in folder

查看:151
本文介绍了如果文件夹中存在多个表,则将多个表从CSV导入到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此问题的扩展程序中:导入多个CSV文件到文件夹的SQL Server

In an extension to this question: Import Multiple CSV Files to SQL Server from a Folder

正如问题所示,我想从CSV文件夹导入多个表到SQL服务器。但事情是在我的情况,是,我可以有多达17个不同的表,都有不同的布局。有些有2列,而其他有10列。

As the question states, I want to import multiple tables from CSV into SQL server from a folder. But the thing is in my case, is that I can have up to 17 different tables, that all have different layouts. Some have 2 columns, while others have 10 for instance.

可以创建一个脚本,该脚本会考虑基于CSV文件名称的不同表格设置,如果表格实际存在于CSV文件中,则可以正确导入夹?

Is it possible to create a script that takes into account the different tables setup based on the name of the CSV file, and import it correct if the table actually exist in the folder?

好的是,CSV文件不会更改名称/布局。但不是每个项目都使用所有17个表。

The good thing is that the CSV files never change names/layout. But not all 17 tables are used in every project.

我想要粗略的代码以及正确的注释,以便我理解它。

I want the rough code along with proper comments so that I understand it.

有人帮我解决这个问题。
提前感谢:)

Someone do please help me out on this one. Thanks a lot in advance :)

推荐答案

这是使用 SQL Server集成服务(SSIS),前提是您在工作环境中可用。 (我会假设你做,或可以安装它。)

This is a perfect situation for using SQL Server Integration Services (SSIS), provided you have it available in your working environment. (I will assume you do, or can get it installed.)

SSIS是一个巨大的平台,所以我不能解释一切关于如何使用它在这里。开始学习的一些好地方是:

SSIS is a huge platform in its own right so I cannot explain everything about how to use it here. Some good places to start learning about it are:

  • The Wikipedia article
  • The MSDN documentation
  • The MSDN beginners' tutorial

还有很多书籍和其他网站。

There are plenty of books and other websites available too.

一般来说,您首先需要创建一个SSIS包,这是一个.dtsx扩展名的文件。您可以使用Microsoft Visual Studio或Microsoft SQL Server数据工具在本地PC上创建它。它有一个合理的用户友好的图形界面。

Broadly speaking, you first need to create an SSIS "package", which is a file of .dtsx extension. You create it on your local PC using Microsoft Visual Studio or Microsoft SQL Server Data Tools. It has a reasonably user-friendly graphical interface.

在此软件包中,您需要为17个可能的文件中的每一个配置数据源,然后将它们映射到目标SQL数据库中的相应表。

Within this package you will need to configure a data source for each of the 17 possible files, then "map" them to the corresponding tables in your target SQL database.

如果您正确设置,如果某些文件不存在,程序包将正常工作 - 它将加载存在的任何文件。 这是为什么SSIS在您的使用情况下是一个不错的选项的关键。

Provided you set it up appropriately, the package will work gracefully if some of the files don't actually exist - it will load whichever are present. This is the crux of why SSIS is a good option in your use-case.

创建包后,有多种使用方式/部署。根据您的意见和问题,我认为最好的方式可能是:

After creating the package, there are multiple ways to use/deploy it. Based on your comments and question, I think the best way for you might be:


  1. 将其上传到SQL Server上的SSIS包存储(使用SSMS对象资源管理器,连接到SSIS实例,而不是SQL Server实例)。为此,您需要在SQL Server平台上安装SSIS组件。

  2. 通过SQL代理创建SQL作业,并带有触发SSIS包的步骤。您可能还希望在该步骤之前或之后执行步骤,以运行准备要加载数据的SQL表的T-SQL代码,或者在加载后对数据进行处理(例如清理和验证数据)。

  3. 您可以手动(通过SQL代理)或使用内置的调度设施(例如,通过SQL代理)来运行作业,而不是运行SQL脚本来设置此整个过程它可以运行每天晚上6点)。

软件包也可以作为一种独立的可执行文件保存在任何文件夹位置,并在Windows中双击运行(不需要再次打开Visual Studio / SSDT)。

Packages can also be saved as a kind of standalone executable, in any folder location, and run from within Windows with a double-click (without needing to open Visual Studio/SSDT again).

这篇关于如果文件夹中存在多个表,则将多个表从CSV导入到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆