Questions tagged [xp-cmdshell]

an extended stored procedure provided by SQL Server, which launches a Windows command shell to run a specified command. Not for general questions about cmd.exe or batch scripting; not related to Windows XP.

The xp_cmdshell extended stored procedure runs the command string given and returns any output as rows of text. The documentation (for SQL Server 2012) can be found here.

For security reasons, xp_cmdshell is disabled by default in recent versions of SQL Server.

264 questions
1
vote
1 answer

SQL Server sqlcmd with a while loop being run with xp_cmdshell

Good day, I have a script where I am pulling server names into a variable which is put into a while loop to form a sqlcmd statement in order to connect to a list of servers and run the query. If I simply Print the statement and run it in a cmd…
J Sharp
  • 23
  • 4
1
vote
1 answer

How to write a FULL XML record to column based on row count

I have a stored procedure that I am using to write custom XML based on a vendors requirements for integrating two systems. I would like to write each record to a column to bypass the char limitation in a sql column. I am including a very simple…
Plane
  • 107
  • 10
1
vote
1 answer

tsql Loop with external query

I am looping through all my databases and aggregating the results into an aggregates database. In my loop I call master.dbo.xp_cmdshell osql C:\whatever.SQL As the loop progresses, the cmdshell takes longer and longer to execute. If I stop the…
shitburg
  • 190
  • 1
  • 9
1
vote
0 answers

BCP using xp_cmdshell to generate outputfile

I'm using the following to generate an output file for some xml content stored in an image data type: EXEC master..xp_cmdshell 'BCP "SELECT XML FROM MyDatabase.dbo.myTable WHERE id = 1 " queryout "C:\temp\exportdir\myfile.xml" -T -S…
adam78
  • 9,668
  • 24
  • 96
  • 207
1
vote
1 answer

Rename list of files from SQL Server 2012

I have a table which has 1000+ records with file name, file path and new file name. I want to rename existing files from FileName to NewFileName. Is there any possibility to rename these files from SQL Server 2012. I am able to do this for one file…
Hemus San
  • 379
  • 1
  • 5
  • 21
1
vote
1 answer

Renaming Files Using XP_CMDSHELL?

The output ScreenshotI have a script, which loops through the SQL Server database table to determine the file type and subsequently rename the files on the network share to add the appropriate extension. Script given below: DECLARE @UNC AS…
iTee
  • 9
  • 6
1
vote
2 answers

Check services at startup of SQL Server

I am trying to check the state of services when SQL Server is started. I am using xp_cmdshell and 'sc query SQLServerAgent | FIND "STATE"' for example to load the output to a global temp table. It works when SQL Server has already started but does…
SQL DBA
  • 11
  • 1
1
vote
1 answer

Run .exe file from SQL Query

i am trying to open a .exe file from within an sql query or Job. im using xp_cmdshell , it is enabled on the server the .exe runs properly when i double click on it from my windows Explorer but when i try to open it using xp_cmdshell it returns to…
Ghassan
  • 351
  • 5
  • 10
  • 19
1
vote
2 answers

sql server xp_cmdshell echo NVARCHAR(MAX) string

I'm trying to use master.sys.xp_cmdshell to echo a extremely large string. I believe the max of varchar which I think is something around 8000 characters is too small so I'm not sure how to accomplish this because xp_cmdshell expects the string to…
sadmicrowave
  • 39,964
  • 34
  • 108
  • 180
1
vote
2 answers

Sql Server xp_cmdshell Access is denied

i'm trying to run some cmd commands via sql query xp_cmdshell all was working fine but when i have tried to run this code : EXEC xp_cmdshell 'net user Test Test2 /ADD' i found this bad result: System error 5 has occurred. NULL Access is…
KingDollar
  • 47
  • 1
  • 6
1
vote
2 answers

xp_cmdshell dir different results when passing command as variable

I am creating a stored procedure to track some server's folders' used space using SQL Server 2008 R2. I have run into an interesting issue for a particular directory. When I run EXEC master.dbo.xp_cmdshell 'dir "\\servername\e$\media\Google" /s…
Dave.Gugg
  • 6,561
  • 3
  • 24
  • 43
1
vote
1 answer

How to escape Newline in ECHO command with sql

i have a Format file for exporting data from SQL Server to local hdd. I have to create this Format file on the remote machine via SQL command. Server srv = new Server(conn); server.ConnectionContext.ExecuteNonQuery("xp_cmdshell 'ECHO 10.0 1 1 …
Relax
  • 283
  • 2
  • 15
1
vote
1 answer

calling a shell script AFTER data is inserted into a table in sybase

I would like to call a shell script AFTER data is inserted into a table (say A) in Sybase 12.5.4. If I use a trigger, on the table A and call xp_cmdshell, then the shell script woudl get called before the insert (data is inserted after the trigger…
1
vote
0 answers

Administrator cannot run a SQL Server query to add a Windows user with "net user /add" using xp_cmdshell

I login to SQL Server with Administrator account and can execute the following query and get the result: xp_cmdshell 'net user' // Works well, and displays all local Windows user accounts But when I want to '/ADD' a new Windows user locally, with…
Mohammad Naji
  • 5,372
  • 10
  • 54
  • 79
1
vote
1 answer

print to PDF using xp_cmdshell

We are trying to use TSQL on SQL 2012 and OS 2012 to print PDF file from a specific directory using variables based on a lookup that populates these variables. The command we have works on SQL 2005 and 2003 datacenter OS. During our upgrade this now…