最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SQL Server數(shù)據(jù)庫bcp導出備份文件應用示例

 更新時間:2014年08月01日 09:57:00   投稿:whsnow  
本節(jié)主要介紹了SQL Server數(shù)據(jù)庫bcp導出備份文件應用,需要的朋友可以參考下
/**
* 授權
*/
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參數(shù)說明

下面是我自己寫的一個存儲過程,可以直接拿去使用
第一步,先要授權。上面有授權的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); /*文件名稱,時間格式,主要是用于記錄數(shù)據(jù)是什么時候?qū)С鰝浞莸?/
declare @table_name varchar(100); /*要導出數(shù)據(jù)的表名*/
declare @sql varchar(1000); /*執(zhí)行業(yè)務數(shù)據(jù)查詢的sql語句*/
/*要備份數(shù)據(jù)的業(yè)務表名*/
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;
-- 導出數(shù)據(jù)到本地文件
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

-- 執(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ù)庫服務實例' -U'數(shù)據(jù)庫登錄用戶名' -P'數(shù)據(jù)庫登錄密碼'
如果運行過程中出現(xiàn)了SQLState = S0002, NativeError=208這個錯誤,則表示是你的 -S服務名寫錯了,
一般常寫錯是因為 沒有加 數(shù)據(jù)庫服務實例,這個可以參考你數(shù)據(jù)庫的連接,照著數(shù)據(jù)庫連接寫就可以。
下圖是我本地的數(shù)據(jù)庫連接,所以我在寫 -S的時候,可以兩種寫法:-S'127.0.0.1\SQLEXPRESS' 或者 -S'PED-VICKY-251\SQLEXPRESS'

3、導出文件中文亂碼,解決方法
bcp 'sql語句' queryout -c -S'IP\數(shù)據(jù)庫服務實例' -U'數(shù)據(jù)庫登錄用戶名' -P'數(shù)據(jù)庫登錄密碼' 改成
bcp 'sql語句' queryout -w -S'IP\數(shù)據(jù)庫服務實例' -U'數(shù)據(jù)庫登錄用戶名' -P'數(shù)據(jù)庫登錄密碼'
即 -c 改成 -w 就行

4、導出后的文件存放目錄,一定要是SQL Server數(shù)據(jù)庫安裝的目錄,不然會出錯

相關文章

最新評論

阳城县| 荥阳市| 涪陵区| 临洮县| 化州市| 邢台市| 二连浩特市| 安龙县| 两当县| 万源市| 澄江县| 凤翔县| 岑溪市| 祁东县| 怀宁县| 讷河市| 永修县| 郸城县| 虹口区| 凤台县| 清河县| 达州市| 滨州市| 西青区| 西和县| 孟津县| 临潭县| 孝感市| 平乡县| 麻城市| 睢宁县| 崇文区| 夏邑县| 历史| 大荔县| 枝江市| 包头市| 昭平县| 黑龙江省| 肇州县| 潮安县|