1. 程式人生 > >SQLSERVER 匯入\匯出sql檔案

SQLSERVER 匯入\匯出sql檔案

1.EXEC master..xp_cmdshell 'bcp test.dbo.P_Aspect in c:\temp1.txt -c -q -S"servername" -U"sa" -P""'
//匯入 

EXEC master..xp_cmdshell 'bcp test.dbo.P_Aspect out c:\temp1.txt -c -q -S"servername" -U"sa" -P""'
//匯出

在匯出時如果報錯:SQL Server阻止了對元件‘xp_cmdshell’的過程‘sys.xp_cmdshell’的訪問
解決辦法:
要想以文字的方式輸出查詢結果,首先需要:
通過查詢分析器,選擇Master資料庫,然後執行以下SQL內容:
sp_configure 'show advanced options',1      --1代表允許,0代表阻止
reconfigure
go
sp_configure 'xp_cmdshell',1
reconfigure
go

然後再執行上面的匯出語句

2.執行sql檔案
C:\>sqlcmd -i  test.sql -d databasename -s 127.0.0.1
注意:這個test.sql檔案要放到對應的碟符中去(這裡就是放入C盤)