【问题标题】:CUSTOM DELIMITER : SQL Server - CSV Export using stored procedure自定义分隔符:SQL Server - 使用存储过程的 CSV 导出
【发布时间】:2021-04-27 08:21:53
【问题描述】:
Begin
Declare @registration_no int = 101
declare @bcp varchar(8000)

set @bcp = 'sqlcmd -s, -W -Q "set nocount on; SELECT * FROM [Student_Management].[dbo].[Student] WHERE registration_no= '+ cast(@registration_no as nvarchar(10))+' " | findstr /v /c:"-" /b > "d:\student.csv"'    

EXEC master..xp_cmdshell @bcp
end

上述语句将表格数据正确导出为 CSV,但不是逗号分隔值,我想要管道(|)分隔输出。我在哪里可以提到自定义分隔符(|)

编辑:

Begin
Declare @registration_no int = 101
declare @bcp varchar(8000)


set @bcp ='sqlcmd -s"|"-W -Q "set nocount on; SELECT * FROM [Student_Management].[dbo].[Student] WHERE registration_no= '+ cast(@registration_no as nvarchar(10))+' " | findstr /v /c:"-" /b > "d:\student.csv"'    
    

EXEC master..xp_cmdshell @bcp

END

【问题讨论】:

  • Larnu 你是对的,请忽略变量名
  • 文档说这个 -s col_separator 指定列分隔符。默认为空格。此选项设置 sqlcmd 脚本变量 SQLCMDCOLSEP。要使用对操作系统具有特殊含义的字符,例如和号 (&) 或分号 (;),请将字符括在引号 (") 中。列分隔符可以是任何 8 位字符。
  • 你能帮忙怎么做我提到示例-s'|'当我这样做时,我得到:消息 402,级别 16,状态 1,第 10 行数据类型 varchar 和 varchar 在“|”中不兼容运算符。
  • 仍然得到数据类型varchar和varchar在“|”中不兼容运算符。
  • 它在进一步的尝试中起作用,但在值之间提供了太多的空白。你能推荐点什么吗?

标签: sql-server csv export delimiter


【解决方案1】:
Begin
Declare @registration_no int = 101
declare @cmd varchar(8000)

set @cmd='sqlcmd -W -s"|", -Q "set nocount on; SELECT * FROM [Student_Management].[dbo].[Student] WHERE registration_no= '+ cast(@registration_no as nvarchar(10))+' " | findstr /v /c:"-" /b > "d:\student.csv"'   

EXEC master..xp_cmdshell @bcp

END

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    相关资源
    最近更新 更多