Bobcares

SQL server xp_cmdshell: How to?

by | Jun 4, 2023

Let us take a closer look at the SQL server xp_cmdshell. At Bobcares with the support of our MSSQL support services, we can give you a detailed note on the usage of the xp_camdshell command.

 

 

What is xp_cmdshell?

The xp_cmdshell is a very powerful extended procedure that can run command line commands (cmd). T-SQ is very useful for running tasks in the operating system such as copying files, creating folders, sharing folders, and so on.

Enable xp_cmdshell in the SQL server

Firstly, we must enable the xp_cmdshell. We’ll need to check to see if SQL Server’s advanced options are active. Run the following procedure in SQL Server Management Studio (SSMS) to accomplish this:     

USE master; 
 GO 
 EXEC sp_configure 'show advanced option

If the configuration value is 0 when we run the command, it means that Advanced Options are not active. Set the advanced option to 1 to enable the Advanced Options. The following example demonstrates how to do it:

USE master;  
GO  
EXEC sp_configure 'show advanced option', '1';  
RECONFIGURE WITH OVERRIDE;  

Xp_cmdshell is one of the advanced options for SQL server files, and we can now enable it. The following example demonstrates how to do so:

EXEC sp_configure 'xp_cmdshell', 1; 
 GO
 RECONFIGURE;

Copy SQL server backups from one folder to another with xp_cmdshell.

Let us look at an example to understand this process better. The following example will demonstrate how to copy multiple backups from one folder to another. Consider the backup is in the c:backup path. The T-SQL commands below will copy the files from c:Backup to c.    

xp_cmdshell 'copy c:\backup c:\shared';  
The output resulting from the process will show that the files are copied.

To store the full path of files in a folder in a SQL Server table using xp_cmdshell

Now let us go through another example that will store in a SQL Server table all the paths of images stored in a specified file.  

Consider the following scenario: we have a large number of images in the c:image folder. Assume we wish to store the whole pathways in a table as follows:

C:\images\1.jpg
C:\images\2.jpg
…etc.

First, we’ll save all of the folder’s paths in a text file called path for SQL server xp_cmdshell command as shown below:

xp_cmdshell 'dir "c:\images" /s/b>c:\sql\path.txt';  

Following this process, the path.txt will now have all of the image’s full paths. After that we need a table to store the paths:

create table images(path varchar(80)

Use the following sentences to import the information from the txt file into the newly constructed table:   

xp_cmdshell 'bcp AdventureWorKs2016CTP3.dbo.images IN c:\sql\path.txt -c -T'

Bcp is the command that can import images from path.txt into the AdventureWorks2016CTP3 database table dbo.images. The -T option connects via a Trust connection (the current Windows Authentication), while the -c option performs character-type operations.

We can check that we were able to successfully import the data to SQL Server by doing the following selection on the photos table:

select * from images.

Next, we’ll make a table with an ID and a column containing the path and picture name columns. This leads to the final steps in the SQL server xp_cmdshell process:

sql server xp_cmdshell

The final step is to import data from the table images into the table images: Insert (imagename,path) into pictures, then pick replace(path,’c:images’,”) as imagename,path from images. The path and picture name will now be stored in a table: choose * from the images.    

 

[Need assistance with similar queries? We are here to help]

 

Conclusion

To sum up we have now seen more about the sql server xp_cmdshell command line with the support of the tech support team.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF