日期:2014-05-18  浏览次数:20536 次

help!遇到一个sql server一个怪事
写了一段SQL代码,在查询分析器里运行,结果不到一万条,把这些结果复制,然后到excel粘贴,发现excel总是有几条格式不对:有几行是空的,还有错位的。

我把查询结果另存为.csv文件,再用excel打开,也是一样的。


有谁遇到过这种奇怪的事?版本是sql server2005

谢谢!

------解决方案--------------------
SQL code

使用语句导出查询结果吧:

执行导出语句前先执行着一段代码:
EXEC sp_configure 'show advanced options',1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO 

之后再执行下面的语句,当然你得修改

将SQL SERVER中查询到的数据导成一个Excel文件
T-SQL代码:
EXEC master..xp_cmdshell 'bcp 库名.dbo.表名out c:\Temp.xls -c -q -S"servername" -U"sa" -P""'
参数:S 是SQL服务器名;U是用户;P是密码

EXEC master..xp_cmdshell 'bcp axzq.dbo.Staff out d:staff.xls -c -q -S"." -U"sa" -P"gazx"'

说明:还可以导出文本文件等多种格式
实例:EXEC master..xp_cmdshell 'bcp saletesttmp.dbo.CusAccount out c:\temp1.xls -c -q -S"pmserver" -U"sa" -P"sa"'
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout C:\ authors.xls -c -Sservername -Usa -Ppassword'

EXEC master..xp_cmdshell 'bcp "select name from axzq..staff order by name"  queryout d:staffName.xls -c -q -S"." -U"sa" -P"gazx"'

------解决方案--------------------
也有可能是系统资源不足所致,重新启动电脑试试..
------解决方案--------------------
可能是你数据里的内容与excel格式特定的解析字符冲突导致的,
具体要查看你那几条错误数据的内容,看看如何替换及避免。
可以试试直接导入到txt,看看那几条数据是否有问题就知道是不是excel的问题了。