Home > SQL Server > SQL SERVER – Execute windows Command from SQL Server using xp_cmdshell

SQL SERVER – Execute windows Command from SQL Server using xp_cmdshell


xp_cmdshell : This system stored procedure is used to execute a windows commands from SQL Server.

This stored procedure is disabled by default in the SQL Server for security reason. But, we can enable this procedure using system configure function.

This is the error message generated by SQL Server when system stored procedure is disabled

pic1To enable xp_cmdshell command, execute the follow query

sp_configure ‘xp_cmdshell’, 1
reconfigure
xp_cmdshell Usage

xp_cmdshell  ‘command_string’  [ , no_output ]

This stored procedure returns the output in rows of text. If we do not want the result then we can use no_output parameter with the procedure during execution.

Example

1. Simple example to execute dir command from SQL Server.

xp_cmdshell ‘Dir c:\*.*’

pic2

2. Now, executing the same command with no_output parameter

xp_cmdshell ‘Dir c:\*.*’, no_output

pic3

This query did not generate any output because we have used no_output parameter.

xp_cmdshell operates synchronously. So, the process control is not returned to the caller until the command-shell command is completed.

Read more information about xp_cmdshell.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: