SQL Server資料庫 bcp匯出備份檔案應用,serverbcp

來源:互聯網
上載者:User

SQL Server資料庫 bcp匯出備份檔案應用,serverbcp
/**
 * 授權
 */
EXEC sp_configure 'show advanced options',1;
go
reconfigure;
go
exec sp_configure 'xp_cmdshell',1;
go
reconfigure;
go

/**匯入指定表的文字檔*/
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參數說明




下面是我自己寫的一個預存程序,可以直接拿去使用
第一步,先要授權。上面有授權的SQL代碼


if exists(select * from sysobjects where type='p' and name='sp_export_posm_data') begin
drop procedure sp_export_posm_data;
end;
go

create procedure sp_export_posm_data 
@file_path varchar(200) /*匯出後檔案存放的路徑*/
as
declare @exec_sql varchar(1000);
declare @file_name varchar(200); /*檔案名稱,時間格式,主要是用於記錄資料是什麼時候匯出備份的*/
declare @table_name varchar(100); /*要匯出資料的表名*/
declare @sql varchar(1000); /*執行業務資料查詢的sql語句*/
/*要備份資料的業務表名*/
declare cur_tables cursor for
select name from sysobjects where 1=1 and type='u'
and name like 'WM_ORDER%' or name like 'WM_PICKING%' or name like 'RP_%'
begin try
open cur_tables;
fetch next from cur_tables into @table_name;
while @@FETCH_STATUS = 0 begin
set @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 begin
set @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;
-- 匯出資料到本地檔案
exec master..xp_cmdshell @exec_sql;
fetch next from cur_tables into @table_name;
end;
close cur_tables; -- 關閉遊標  
deallocate cur_tables;-- 釋放遊標 
end try
begin catch
close cur_tables; -- 關閉遊標  
deallocate cur_tables;-- 釋放遊標 
end catch;
go


-- 執行預存程序,進行測試
exec sp_export_posm_data '';

注意事項:
1、查詢語句的文法 select * from [資料庫名]..[表名];
      如果運行過程中出現了SQLState = S1000, NativeError=0這個錯誤,這表示是你的資料庫名或表名寫錯了
2、bcp 'sql語句' queryout -c -S'IP\資料庫服務執行個體' -U'資料庫登入使用者名稱' -P'資料庫登入密碼'
      如果運行過程中出現了SQLState = S0002, NativeError=208這個錯誤,則表示是你的 -S服務名寫錯了,
      一般常寫錯是因為 沒有加 資料庫服務執行個體,這個可以參考你資料庫的串連,照著資料庫連接寫就可以。
      是我本地的資料庫連接,所以我在寫 -S的時候,可以兩種寫法:-S'127.0.0.1\SQLEXPRESS' 或者 -S'PED-VICKY-251\SQLEXPRESS'
       

3、匯出檔案中文亂碼,解決方案
      bcp 'sql語句' queryout -c -S'IP\資料庫服務執行個體' -U'資料庫登入使用者名稱' -P'資料庫登入密碼'  改成
      bcp 'sql語句' queryout -w -S'IP\資料庫服務執行個體' -U'資料庫登入使用者名稱' -P'資料庫登入密碼'
      即 -c 改成 -w 就行
   
4、匯出後的檔案存放目錄,一定要是SQL Server資料庫安裝的目錄,不然會出錯
匯入SQLSERVER備份檔案問題,

首先要在自己的企業管理器中建立一個資料庫,這個庫最好和其備份的資料庫名相同,不同也可以,但恢複有點複雜。

建完,如果資料庫名與備份資料庫名相同,則直接在建立的資料庫上右鍵>所有任務>還原資料庫,從裝置,選擇裝置,添加,選擇哪個備份,一路確定就好了

如果資料庫名與備份資料庫名不同,則直接在建立的資料庫上右鍵>所有任務>還原資料庫,從裝置,選擇裝置,添加,選擇哪個備份,確定,在還原資料庫頁停住,點選項頁簽,在移至物理檔案名稱處將檔案名稱改成你建立的資料庫的名字,記得後面有個底線‘_’+‘Data.MDF’…………
兩個都要改,一個Data.MDF,一個Log.LDF,然後選強制還原,確定,OK
 
用bcp命令將表格檔案匯入sql server

試下:
bcp "data2005..dbo.2009_12_31_C" in "2009_12_31_C.xls" -c -t -U "sa" -P "_sql" -S "nanhui"

data2005後應該是兩個點
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.