将excel中的数据集与SQL Server表进行比较

时间:2015-07-02 22:32:02

标签: c# sql-server sql-server-2008 sql-server-2008-r2 sql-server-2012

我有一个数据集的摘录,如下所示,从mysql表连接到Excel。

╔════════════════════════════════════════════════════════════════╗
║ student_no     name  age subject   mark  status   updated_date ║
╠════════════════════════════════════════════════════════════════╣
║ 1              aaa   18  Subject1   50    A      2015-07-02    ║
║ 2              bbb   19  Subject1   50    A      2015-07-02    ║
║ 3              ccc   20  Subject3   60    A      2015-07-02    ║
║ 4              ddd   21  Subject4   50    A      2015-07-02    ║
║ 5              fff   22  Subject5   70    A      2015-07-02    ║
╚════════════════════════════════════════════════════════════════╝

我需要将上述数据集的每一行与我的SQL表(如下面的查询)进行比较,如果SQL表中没有一行数据集值,那么我需要更改状态来自&#39 ; A'到了我'在上面的数据集的特定行中。

SQL查询

select 
    std.student_number, std.student_name,
    sa.subject_name, ma.mark 
from 
    student std
join 
    subject sa ON std.student_no = sa.student_no
join 
    marks ma ON sa.subject_id=ma.subject_id
where 
    (std.student_name = 'aaa' 
     and std.age = 18 
     and sa.subject = 'Subject1')

select 
    std.student_number, std.student_name,
    sa.subject_name, ma.mark 
from 
    student std
join 
    subject sa ON std.student_no = sa.student_no
join 
    marks ma ON sa.subject_id = ma.subject_id
where 
    (std.student_name = 'bbb' 
     and std.age = 19 
     and sa.subject = 'Subject1')

我是否需要编写任何.net应用程序来执行此操作,还是可以通过SQL Server本身执行此操作?

1 个答案:

答案 0 :(得分:0)

您应该使用SSIS(SQL Server Integration Services)并创建一个项目,该项目将获取Excel文件并将其转储到新表中,然后创建一个查询以将原始表与在新表中导入的excel表进行比较表