我希望将SQL查询导出到SSIS中的ExecuteProcessTask中的csv平面文件中。
我无法看到带有标题的导出,分隔符并将其限定为文本。我试过用sqlcmd和bcp。
关于信息,我不得不使用SELECT *,因为FROM中的视图是一个变量,我必须显示所有的列。
使用sqlcmd:
sqlcmd -S ServerName -d dbName -E -Q "SELECT * FROM vPBI_Tasks WHERE [project Leader] like 'ProjectLeaderName'" -o "exportFile.csv" -W -s";"
提取结果:
Scope;Project type;Activity type;OBS;Customer;Contr...
-----;------------;-------------;---;--------;-----...
ESP;ESP - Amendment;NULL;NULL;GSA;ESP_Amendment#13;...
ESP;ESP - Amendment;NULL;NULL;GSA;ESP_Amendment#13;...
ESP;ESP - Amendment;NULL;NULL;GSA;ESP_Amendment#13;...
我想:
"Scope";"Project type";"Activity type";"OBS";"Customer";"Contra..."
ESP";"ESP - Amendment";"NULL";"NULL";"GSA";"ESP_Amendment#13";""
ESP";"ESP - Amendment";"NULL";"NULL";"GSA";"ESP_Amendment#13";""
ESP";"ESP - Amendment";"NULL";"NULL";"GSA";"ESP_Amendment#13";""
与bcp:
bcp "SELECT * FROM vPBI_Resources WHERE [project Leader] like 'ProjectLeaderName'" queryout "exportFile.csv" -c -t ; -S ServerName -T
结果:
发布于 2019-07-24 17:52:37
请参阅对先前类似请求的答复:
SQL Server BCP Bulk insert Pipe delimited with text qualifier format file
本质上,您需要使用BCP格式文件。生成BCP命令时,请包括-f选项并指定格式文件的位置。在格式文件中,不只是将分隔符指定为分号字符,而是指定为";“(这是两个带有分号的dbl-引号字符)。
还有一点比这更多,但链接还有其他的。
要获得包含的标题,您只需要本质上使用2个查询即可。一个查询将用于标题,另一个查询将用于详细记录。您可以使用BCP的"queryout“选项将这两个查询”联合“在一起。您必须将所有详细数据转换为varchar数据类型,以便将它们一起查询到一个文件中。但是,既然您已经开始使用文本文件了,这就不应该引起问题。还有其他的答案详细说明了如何获得以这种方式包含的标题。我很快就会添加一个编辑。您还可以将头记录和细节记录作为两个单独的文件(2个单独的bcp命令)查询出来,并将它们合并到一个OS/script命令中。
发布于 2019-07-26 09:24:49
我确实考虑过这个解决方案,但我对在行的开头和结尾加上双引号的问题感到困惑。
我找到的解决方案是C#中的一个脚本。http://neil037.blogspot.com/2013/07/ssis-script-task-to-export-data-from.html
我将C#代码放在下面,它将用于其他人:)。
public void Main()
{
String filePath = Dts.Variables["User::temporyExportFilePath"].Value.ToString();
Dts.TaskResult = (int)ScriptResults.Success;
CreateCSVFile(GetTableData(), filePath);
}
public DataTable GetTableData()
{
String sqlQuery = Dts.Variables["User::sqlQuery"].Value.ToString();
String connectionString = Dts.Variables["User::stringDatabaseConnection"].Value.ToString();
SqlConnection connect = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(sqlQuery, connect);
cmd.CommandType = CommandType.Text;
SqlDataAdapter adap = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adap.Fill(dt);
return dt;
}
public void CreateCSVFile(DataTable dt, string strFilePath)
{
StreamWriter sw = new StreamWriter(strFilePath, false);
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
// Write text qualifier double-quote + value + double-quote
sw.Write("\"" + dt.Columns[i] + "\"");
if (i < iColCount - 1)
{
//Parser
sw.Write(";");
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
// Write text qualifier double-quote + value + double-quote
sw.Write("\"" + dr[i].ToString() + "\"");
}
if (i < iColCount - 1)
{
//Parser
sw.Write(";");
}
}
sw.Write(sw.NewLine);
}
//Close file and all data writing
sw.Close();
}
https://stackoverflow.com/questions/57180197
复制相似问题