SQLServer數(shù)據(jù)庫中怎么利用bcp導(dǎo)出備份文件

SQL Server數(shù)據(jù)庫中怎么利用bcp導(dǎo)出備份文件,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)是專業(yè)的石城網(wǎng)站建設(shè)公司,石城接單;提供成都網(wǎng)站制作、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行石城網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!

/*** 授權(quán)*/EXEC sp_configure 'show advanced options',1;goreconfigure;goexec sp_configure 'xp_cmdshell',1;goreconfigure;go/**導(dǎo)入指定表的文本文件*/EXEC master..xp_cmdshell 'bcp dbname..tablename in d:\DT.txt -c -Sservername -Usa -Ppassword'exec master..xp_cmdshell 'bcp "select * from dbname..tablename" queryout "D:\20140528.xls"-c -Sservername -Uuser -Ppassword'

xp_cmdshell參數(shù)說明

下面是我自己寫的一個存儲過程,可以直接拿去使用第一步,先要授權(quán)。上面有授權(quán)的SQL代碼

if exists(select * from sysobjects where type='p' and name='sp_export_posm_data') begindrop procedure sp_export_posm_data;end;gocreate procedure sp_export_posm_data @file_path varchar(200) /*導(dǎo)出后文件存放的路徑*/asdeclare @exec_sql varchar(1000);declare @file_name varchar(200); /*文件名稱,時間格式,主要是用于記錄數(shù)據(jù)是什么時候?qū)С鰝浞莸?/declare @table_name varchar(100); /*要導(dǎo)出數(shù)據(jù)的表名*/declare @sql varchar(1000); /*執(zhí)行業(yè)務(wù)數(shù)據(jù)查詢的sql語句*//*要備份數(shù)據(jù)的業(yè)務(wù)表名*/declare cur_tables cursor forselect name from sysobjects where 1=1 and type='u'and name like 'WM_ORDER%' or name like 'WM_PICKING%' or name like 'RP_%'begin tryopen cur_tables;fetch next from cur_tables into @table_name;while @@FETCH_STATUS = 0 beginset @file_name = '';set @file_path = '';set @sql = 'select * from DHL_POSM_WS..'+@table_name;set @sql += ' where 1=1 and DATEDIFF(MONTH,MODIFY_TIME,GETDATE())>10';print @sql;set @exec_sql = ' bcp "'+@sql+'" queryout ';if ''=@file_path beginset @file_path = 'D:\Program Files (x86)\Microsoft SQL Server\';end;print '111111';set @file_name = @table_name+'_'+CONVERT(varchar(100), GETDATE(), 112)+'.xls';set @file_path = @file_path + @file_name; /*文件路徑*/print '2222222';set @exec_sql = @exec_sql +'"'+@file_path+'"';set @exec_sql = @exec_sql +' -c -S"127.0.0.1\SQLEXPRESS" -U"DHL_POSM_WS" -P"DHLposm"';print @exec_sql;-- 導(dǎo)出數(shù)據(jù)到本地文件exec master..xp_cmdshell @exec_sql;fetch next from cur_tables into @table_name;end;close cur_tables; -- 關(guān)閉游標(biāo) deallocate cur_tables;-- 釋放游標(biāo) end trybegin catchclose cur_tables; -- 關(guān)閉游標(biāo) deallocate cur_tables;-- 釋放游標(biāo) end catch;go-- 執(zhí)行存儲過程,進(jìn)行測試exec sp_export_posm_data '';

注意事項:

1、查詢語句的語法 select * from [數(shù)據(jù)庫名]..[表名];如果運行過程中出現(xiàn)了SQLState = S1000, NativeError=0這個錯誤,這表示是你的數(shù)據(jù)庫名或表名寫錯了2、bcp 'sql語句' queryout -c -S'IP\數(shù)據(jù)庫服務(wù)實例' -U'數(shù)據(jù)庫登錄用戶名' -P'數(shù)據(jù)庫登錄密碼'如果運行過程中出現(xiàn)了SQLState = S0002, NativeError=208這個錯誤,則表示是你的 -S服務(wù)名寫錯了,一般常寫錯是因為 沒有加 數(shù)據(jù)庫服務(wù)實例,這個可以參考你數(shù)據(jù)庫的連接,照著數(shù)據(jù)庫連接寫就可以。下圖是我本地的數(shù)據(jù)庫連接,所以我在寫 -S的時候,可以兩種寫法:-S'127.0.0.1\SQLEXPRESS' 或者 -S'PED-VICKY-251\SQLEXPRESS'

3、導(dǎo)出文件中文亂碼,解決方法bcp 'sql語句' queryout -c -S'IP\數(shù)據(jù)庫服務(wù)實例' -U'數(shù)據(jù)庫登錄用戶名' -P'數(shù)據(jù)庫登錄密碼' 改成bcp 'sql語句' queryout -w -S'IP\數(shù)據(jù)庫服務(wù)實例' -U'數(shù)據(jù)庫登錄用戶名' -P'數(shù)據(jù)庫登錄密碼'即 -c 改成 -w 就行4、導(dǎo)出后的文件存放目錄,一定要是SQL Server數(shù)據(jù)庫安裝的目錄,不然會出錯

看完上述內(nèi)容,你們掌握SQL Server數(shù)據(jù)庫中怎么利用bcp導(dǎo)出備份文件的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

文章標(biāo)題:SQLServer數(shù)據(jù)庫中怎么利用bcp導(dǎo)出備份文件
網(wǎng)站URL:http://muchs.cn/article8/pdghop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站關(guān)鍵詞優(yōu)化、全網(wǎng)營銷推廣、網(wǎng)站營銷服務(wù)器托管、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營