--處理的預存程序create proc p_export@path nvarchar(1000) --匯出的文字檔儲存的目錄asdeclare @s nvarchar(4000)if isnull(@path,'')='' set @path='c:\'else if right(@path,1)<>'\' set @path=@path+'\'
--用遊標構建每條記錄的bcp匯出語句,BCP的文法參考sql線上說明declare tb cursor localforselect 'BCP "select detail from ' +quotename(db_name()) +'..tb where id=' +quotename(id,N'''') +'" queryout "'+@path +id+'.txt" /T /w'from tbopen tb fetch tb into @swhile @@fetch_status=0begin --調用xp_cmdshell預存程序執行bcp進行匯出處理 exec master..xp_cmdshell @s,no_output fetch tb into @sendclose tbdeallocate tbgo