2010-12-22

SQL SELECT 輸出至文字檔案

SQL SELECT 輸出至文字檔案

設定 SQL Server 的介面區組態, 啟用 xp_cmdshell

( 請注意啟用 xp_cmdshell 會讓 sql injection 有機會寫入檔案至 server 上 )

執行 SQL

EXEC xp_cmdshell'bcp "select * from Northwind.dbo.Orders " queryout "c:\SQLoutput.txt" -c -T -x'

( "c:\SQLoutput.txt" 是 SQL Server 本機上的檔案路徑 )


( 設定 SQL Server 的介面區組態, 啟用 xp_cmdshell , 可以用以下指令 )

EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE

( 停用 xp_cmdshell )

EXEC master.dbo.sp_configure 'show advanced options', 0
RECONFIGURE
EXEC master.dbo.sp_configure 'xp_cmdshell', 0
RECONFIGURE

沒有留言:

熱門文章