Running Programs


Before Microsoft included support for .NET framework, OLE Automation, and COM in SQL Server, administrators ran command-prompt programs and commands using the xp_cmdshell extended stored procedure:

 xp_cmdshell {'command'} [, no_output] 

When xp_cmdshell is executed, a command string is passed to the command shell of the operating system to be executed. Any rows of text that are normally displayed by the command shell are returned by the extended stored procedure as a result set. There is also an option to ignore the output.

The status of the execution is returned as an output parameter of the extended stored procedure. Its value is set to 0 if execution was successful and 1 if execution failed.

Figure 21-1 shows the use of the command-prompt instruction to list files in the Backup folder. This output can be received in a temporary table and further processed in Transact-SQL code.

image from book
Figure 21-1: Using xp_cmdshell to run commands and programs

The following batch-copies files from the Backup folder to another drive:

 exec master..xp_cmdshell 'copy e:\w2kPro~l\Micros~l\'                     + 'MSSQL\BACKUP\*.* m:', no_output 




Microsoft SQL Server 2005 Stored Procedure Programming in T-SQL &  .NET
Microsoft SQL Server 2005 Stored Procedure Programming in T-SQL & .NET
ISBN: 0072262281
EAN: 2147483647
Year: 2006
Pages: 165

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net