简体   繁体   English

用ssis导出多个表

[英]export multiple table with ssis

I have 2 tables are like this (Table1 and Table2) 我有2个表是这样的(表1和表2)

ID  NAME             No      Addrress     Notes
------------        ----------------------------
1   John            111      USA          Done
2   Steve           222      Brazil       Done

Now I want to create a SSIS package which will create a csv file like: 现在,我想创建一个SSIS包,它将创建一个csv文件,例如:

Table1;ID;NAME             
Table2;No;Addrress;Notes  
"Detail1";"1";"John";"2";"Steve"           
"Detail2";"111";"USA";"Done";"222";"Brazil";"Done"

Can we achieve the same output? 我们可以实现相同的输出吗? I have searched on google but haven't found any solution. 我在Google上进行了搜索,但未找到任何解决方案。

Please help .... 请帮忙 ....

You can create a script task to generate a CSV file for you which can handle your issue: 您可以创建一个script task来为您生成一个可以处理您的问题的CSV文件:

You can try this: 您可以尝试以下方法:

 SqlConnection sqlCon = new SqlConnection("Server=localhost;Initial Catalog=LegOgSpass;Integrated Security=SSPI;Application Name=SQLNCLI11.1");

        sqlCon.Open();
        SqlCommand sqlCmd = new SqlCommand(@"Select ID,Name from dbo.Table1", sqlCon);
        SqlDataReader reader = sqlCmd.ExecuteReader();


        string fullpath = @"C:\Users\thoje\Desktop\stack\New folder\table1.csv";
        StreamWriter sw = new StreamWriter(fullpath);
        object[] output = new object[reader.FieldCount];

        for (int i = 0; i < reader.FieldCount; i++)
            output[i] = reader.GetName(i);

        sw.WriteLine(@"Table1;"+string.Join(";", output));

        List<object> values = new List<object>();
        while (reader.Read())
        {
            reader.GetValues(output);

            values.Add($"\"{output[0]}\"");
            values.Add($"\"{output[1]}\"");



        }

        sw.WriteLine(@"""Detail1"";"+ string.Join(";", values));
        sw.Flush();
        sw.Close();
        reader.Close();
        sqlCon.Close();


        Dts.TaskResult = (int)ScriptResults.Success;

Result: 结果:

在此处输入图片说明

You really should put in your question what you have tried so far, it helps out a lot and makes it more fun to help people. 您确实应该提出一个问题,到目前为止您已经尝试过什么,这对您有很大帮助,让帮助人们变得更加有趣。

The two ways I can think of in t-sql to solve this still need you to specify in your code what your column names are. 我可以在t-sql中想到的两种解决方法仍然需要您在代码中指定列名是什么。 You can get around this with using dynamic SQL and creating a view that spits out data in the same fashion for all the tables you need. 您可以使用动态SQL来解决此问题,并创建一个视图,以所需的所有表的相同方式吐出数据。

If SSIS is more your thing you could use the dynamic approach with BIML. 如果您更喜欢SSIS,则可以将动态方法与BIML结合使用。

--Option 1 (SQL Server 2008 R2 and later)
with Table1 AS (
SELECT * FROM (values(1,'John'),(2,'Steve')) AS x(ID,NAME)
)
,Table2 AS (
SELECT * FROM (values(111,'USA','Done'),(222,'Brazil','Done'))AS y(No,Addrress,Notes)
)
SELECT '"Detail1"'+ CAST(foo as VARCHAR(4000))
FROM (
SELECT ';"' + CAST(ID AS VARCHAR(4))+'";"' + [NAME] +'"'  FROM Table1 FOR XML PATH('')
) AS bar(foo)
UNION ALL
SELECT '"Detail2"'+ CAST(foo as VARCHAR(4000))
FROM (
SELECT ';"' + CAST([No] AS VARCHAR(4))+'";"' + [Addrress] +'";"' + [Notes] +'"'  FROM Table2 FOR XML PATH('')
) AS bar(foo)



--Option 2 (SQL Server 2017 and later)
with Table1 AS (
SELECT * FROM (values(1,'John'),(2,'Steve')) AS x(ID,NAME)
)
,Table2 AS (
SELECT * FROM (values(111,'USA','Done'),(222,'Brazil','Done'))AS y(No,Addrress,Notes)
)
SELECT '"Detail1";' + STRING_AGG('"'+CAST(ID AS varchar(4))+'";"'+[NAME]+'"',';') FROM Table1
UNION ALL
SELECT '"Detail2";' + STRING_AGG('"'+CAST([No] AS varchar(4))+'";"'+[Addrress]+'";'+'"'+[Notes]+'"',';') FROM Table2
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM