Skip Navigation Links
Home
Functions
Tips & Tricks
SQL Server 2005
SQL Server 2008
SQL Server 2012
FAQ
Forums
Practice Test
Bookstore
Tip of the Day : 2 Ways to Execute Operating System Commands From SQL Server Management Studio
Welcome to SQL Server Helper!!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

 
SQL Server Tip of the Day - October 30, 2014
2 Ways to Execute Operating System Commands From SQL Server Management Studio
There are two ways of executing operating system commands from SQL Server Management Studio.  The first method is with the use of the [dbo].[xp_cmdshell] system extended stored procedure.  The xp_cmdshell system extended stored procedure spawns a Windows command shell and passes in a string for execution.  Any output is returned as rows of text.

xp_cmdshell { 'command_string' } [, no_output ]
‘command_string’ is the string that contains a command to be passed to the operating system.  The data type of ‘command_string’ is VARCHAR(8000) or NVARCHAR(4000), with no default value.  The no_output parameter is optional which specifies that no output should be returned to the client.

The Windows process spawned by xp_cmdshell has the same security rights as the SQL Server service account.  xp_cmdshell operates synchronously, that is, control is not returned to the caller until the command-shell command is completed.

Here’s an example of getting the list of SQL scripts from a certain directory using the “dir” operating system command:

EXECUTE [master].[dbo].[xp_cmdshell] 'dir C:\Scripts\*.sql'
Here’s another example which returns the list of SQL Servers on the network:

EXECUTE [master].[dbo].[xp_cmdshell] 'sqlcmd -Lc'
The second method of executing operating system commands from SQL Server Management Studio is with the use of the SQLCMD Mode.  To enable SQLCMD mode, go to the Query menu option of SQL Server Management Studio and select SQLCMD Mode or click on the SQLCMD Mode icon in the toolbar (the icon that has the window in the background and an exclamation point in the foreground).  The sqlcmd utility allows the entry of Transact-SQL statements, system procedures and script files at the command prompt.  But with the SQLCMD Mode, you are not limited to executing the sqlcmd utility.  You can also execute any operating system commands by simply prefixing the command with 2 exclamation points (!!).

Using the same examples with the xp_cmdshell extended stored procedure, here’s how to get a list of SQL Server scripts from a certain directory using the “dir” operating system command:

!!dir C:\Scripts\*.sql
Here’s another example which returns the list of SQL Servers on the network:

!!sqlcmd -Lc
 
SQL Server 2012
What's New in SQL Server 2012
SQL Server 2012 Articles
 
SQL Server 2008
What's New in SQL Server 2008

A collection of articles related to what's new with SQL Server 2008:

SQL Server 2008 Articles

A collection of articles related to SQL Server 2008:

 
User-Defined Functions
Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions
A collection of useful table-valued user-defined functions that can be used to join with other tables.
SQL Server Built-in Functions
A reference to all built-in functions available within SQL Server grouped into categories.
 
Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

 
Frequently Asked Questions
SQL Server

A collection of frequently asked questions about SQL Server grouped by categories:

SQL Server 2005

A collection of frequently asked questions about SQL Server 2005 grouped by categories: