有如下幾種方法:
1.直接復(fù)制結(jié)果集并粘貼到新建的EXCEL中或?qū)⒔Y(jié)果另存為csv格式的文件,EXCEL也能打開CSV文件;
2.采用SQL Server的DTS導(dǎo)出
3.采用bcp工具導(dǎo)出。語法詳見SQL Server2008聯(lián)機叢書。例:
引用
exec master..xp_cmdshell 'bcp " select * from dbName..tableName" queryout c:\temp.xls -c -U "userName" -P "password"'
其中的參數(shù)按順序分別為:數(shù)據(jù)庫名、表名、導(dǎo)出EXCEL的完整路徑、數(shù)據(jù)庫用戶名、密碼
注意其中的引號。
上述腳本中使用到了master數(shù)據(jù)庫中的存儲過程xp_cmdshell,在SQL Server 2008中默認是禁用的,需要先啟用,否則會報錯??墒褂靡韵履_本開啟xp_cmdshell:
-
- EXEC sp_configure 'show advanced options', 1
- GO
-
- RECONFIGURE
- GO
- EXEC sp_configure 'xp_cmdshell', 1
- GO
- RECONFIGURE
- GO
--調(diào)用sp_configure配置EXEC sp_configure 'show advanced options', 1GO--更新配置信息RECONFIGUREGOEXEC sp_configure 'xp_cmdshell', 1GORECONFIGUREGO