1. 程式人生 > >SQL server 將資料匯出到檔案方法

SQL server 將資料匯出到檔案方法

這裡使用xp_cmdshell命令。Mark一下。

注:如果操作SQL Server的使用者沒有SP_configure的使用許可權,需要將對應使用者新增"sysadmin"角色。

1、首先啟動該命令,方法如下:

SP_CONFIGURE'show advanced options',1 RECONFIGURE SP_CONFIGURE 'xp_cmdshell',1 RECONFIGURE

2、將整個表資料匯出到檔案,方法如下:

exec master..xp_cmdshell ' bcp database.table out  d:/data.csv -c -t -T '

3、將查詢結果匯出到檔案,方法如下:


 exec master..xp_cmdshell ' bcp "select * from test..PAD6_holding_s where TradingDay = convert(varchar(19),GETDATE(),101)" queryout "D:/text.csv" /c -U test_pan -P 123abc'