简体   繁体   English

SQL Server将存储过程结果导出为CSV文件,保留char(9)格式

[英]SQL Server export stored procedure results to CSV file keeping char(9) format

I'm having a problem with exporting the results of a stored procedure to a csv file and keeping the results as a 9 character string. 将存储过程的结果导出到csv文件并将结果保留为9个字符串时出现问题。 The results of the stored procedure is a simple one column output which looks fine when executed in SSMS but the returned values in the csv that have leading zeros are being returned without the zeros. 存储过程的结果是一个简单的单列输出,当在SSMS中执行时看起来不错,但是csv中具有前导零的返回值将不带零返回。 The table column is type varchar(13) and I have done a convert to try and keep the leading zeros from being dropped but no luck. 表列的类型为varchar(13) ,我进行了一次转换,以尝试防止丢失前导零,但没有运气。

Here is the stored procedure: 这是存储过程:

SELECT DISTINCT
convert(char(8),n.NIIN)
FROM IMMS_ELEC.dbo.NIINList n

Here is the simple BCP script I'm using: 这是我正在使用的简单BCP脚本:

DECLARE @string AS NVARCHAR(4000)
SELECT    @string = 'BCP "exec CPLINK_Dev.dbo.spSelectLOG_NiinDistinct"
QUERYOUT:\data.csv -c -T -t'
exec master.dbo.xp_cmdshell @string

Excel loves to think it knows how to format your data better than you do... Here's a trick you can use to outsmart it. Excel喜欢认为它比您能更好地格式化数据。这是一个可以使数据智能化的技巧。 Open a new spreadsheet, select all cells and change the type to text (from general), then copy your data from notepad (or SSMS), paste it into Excel, and use text to columns if you have to... Excel should stop messing with your formats then. 打开一个新的电子表格,选择所有单元格并将类型更改为文本(从常规),然后从记事本(或SSMS)复制数据,将其粘贴到Excel中,并在必要时使用文本到列... Excel应该停止然后弄乱您的格式。

You can also probably do the same thing with import data from Excel, but I find the first much more effective and you can use it to copy and paste directly from SSMS grid results as well. 您也可以对从Excel导入数据执行相同的操作,但是我发现第一个更为有效,您也可以使用它直接从SSMS网格结果中复制和粘贴。

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

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