Using xp_cmdshell

One of the most useful, and potentially dangerous, extended stored procedures provided with SQL Server 2012 is xp_cmdshell. xp_cmdshell can execute any operating system command or program available on the SQL Server system, as long as it is a console program that doesn’t require user input. xp_cmdshell accepts a varchar(8000) (or nvarchar(4000)) value as the command string to be executed, and it returns the results of the command as a single nvarchar(255) column. The full syntax of xp_cmdshell is as follows:

xp_cmdshell { 'command_string' } [ , no_output ]

If the no_output option is specified, the results from the command are not displayed.

The following example uses xp_cmdshell to list the files ...

Get Microsoft® SQL Server 2012 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.