BCP Scripts

A BCP script is simply a batch file or a Windows Script Host file that contains BCP commands. Example 10-3 shows examples of how to run BCP using various scripting options. If you do not know how to use batch files or Windows Script Host, two great resources are Windows Command-Line Administrator’s Pocket Consultant (Microsoft Press, 2005) and Windows 2000 Scripting Guide (Microsoft Press, 2002).

Example 10-3. Using BCP in a Script

sched-export.bat

@echo off                            
@if not "%OS%"=="Windows_NT" goto :EXIT      
bcp pubs..customer out customers.txt -c -t, -T
:EXIT

sched-export.vbs

'Nightly Bulk Copy export for the customers table      
'Writes output to cust.txt and errors to err.txt       
Set ws = WScript.CreateObject("WScript.Shell") 
ret = ws.Run("bcp pubs..customers out cust.txt -c -t, -T
-eerr.txt",0,"TRUE")

sched-export.js

\Nightly Bulk Copy export for the customers table      
\Writes output to cust.txt and errors to err.txt       
var ws = WScript.CreateObject("WScript.Shell"); 
ret = ws.Run("bcp pubs..customers out cust.txt -c -t, -T -
eerr.txt",0,"TRUE")

After you create a script file for the bulk copy command, you can schedule it as a task to run on your system. To schedule these scripts to run every night at midnight, for example, use the following commands:

AT 00:00 /every:M,T,W,Th,F,S,Su "sched-export.bat"
AT 00:00 /every:M,T,W,Th,F,S,Su "cscript //B sched-export.js"
AT 00:00 /every:M,T,W,Th,F,S,Su "cscript //B sched-export.vbs"

Tip

Tip

For more information on scheduling tasks, refer to Microsoft Windows Server 2003 Administrator’s Pocket Consultant, 2nd ed. (Microsoft Press, 2005) or Microsoft Windows XP Administrator’s Pocket Consultant, 2nd ed. (Microsoft Press, 2005).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset