溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-08-05 14:51:17 來源:億速云 閱讀:313 作者:Leah 欄目:數(shù)據(jù)庫

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

/*** 授權(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í)行存儲過程,進行測試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)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI