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
0
votes
1 answer

Bulk export using BCP on Azure

Within sql server, in a scheduled job, I was using the below command to create a csv file and upload it in a directory. EXEC('EXEC xp_cmdshell ''bcp dbo.sales out "f:\db\salesbackUp\29July2022.csv" -T -c -t"," -C ACP -r\n…
SAJID BHAT
  • 11
  • 3
0
votes
1 answer

How to resolve "...Error occurred during the execution of xp_cmdshell"?

The engineers uses this Access based app that takes data from SQL Server 2005. The users download a file containing details of parts etc make amendments and upload it again. When the file is upload(checked-in) the information such as the modified…
Brute
  • 33
  • 5
0
votes
1 answer

utf-8 path in master..xp_cmdshell command

How can I execute master..xp_cmdshell in SQL Server with path that includes utf-8 chars the code works well in another servers and in cmd but not in sql EXEC master..xp_cmdshell 'dir \\my-drive\users\myUser\utf8_folder_name\' Pay attention to the…
Ron
  • 21
  • 6
0
votes
0 answers

Linked servers, results query xp_cmdshell to table migration SQL Server 2005 to SQL Server 2019

I'm migrating procedures from Microsoft SQL Server 2005 to Microsoft SQL Server 2019 and I got stuck while trying insert query result xp_cmdshell in linked servers to table I'm out of ideas Old solution in Microsoft SQL Server 2005: INSERT INTO…
0
votes
1 answer

xcopy throws error on SQL Server (xp_cmdshell Script)

on SQL we are creating a new folder by copying and existing folder by xp_cmdshell script. SQL Script: declare @cmd sysname set @cmd = 'xcopy /t /e /i "C:\Rudresh\Process\001" "C:\Rudresh\Process\002" /O /X /H /K' exec…
Rudresh Kundur
  • 43
  • 1
  • 10
0
votes
0 answers

Export a csv using xp_cmdshell

I'm trying export my results using xp_cmdshell but I recive this error : "Sqlcmd: error: error while opening or using file (my_path)". This is what I've wrote on sqlserver: exec xp_cmdshell 'sqlcmd -S my_server -d my_db -E -Q "my_query" -s "," -o…
Antonio B
  • 1
  • 3
0
votes
0 answers

Renaming a XML File from another server using T-SQL

I'm looking for a way to rename an XML file from another server. I'm currently able to do this locally with the code below but now need to rename the file from a different server. DECLARE @Rename NVARCHAR(2000) SET @Rename = 'ren…
Theren
  • 43
  • 4
0
votes
0 answers

Execute a batch file from SQL server using xp_cmdshell

I have a .bat file that iterates through it's residing director and takes the file name and creates a series of insert into select openrowset statements that is written to another file for later use. I am trying to execute this .bat file in SQL…
0
votes
1 answer

SQL Server Execute with command line tool returns missing field information

I want to execute a command line utility using xp_cmdshell and return the results in CSV format for processing in a SQL Server table. The command line with options/parameters: tasklist /v /FO:CSV The SQL script line is: EXEC xp_cmdshell 'tasklist…
rkapukaya
  • 77
  • 1
  • 8
0
votes
0 answers

ZIP file with EXEC master.dbo.xp_cmdshell

I am using the following code to zip a csv file and pdf. My issue is that I get an error that says: Msg 102, Level 15, State 1, Line 36 Incorrect syntax near '+'. the above error refers to the first '+ @Imagename +' declare @Imagename…
JCW
  • 17
  • 5
0
votes
0 answers

Information about xp_cmdshell

I have enabled Xp_cmdshell extended stored procedure successfully. I have typed the command EXEC xp_cmdshell 'del C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\testdelete\CallRecordings\2020\05\05\Call' and despite the file…
0
votes
1 answer

Copy group of files without looping and copying each file individually

I have about 1.8 mln files in a folder. These need to be copied to another folder. There is a table [staging].[filelist] that tells me which files to copy. I have a SQL statement that will get a list of filenames from that table and then copy those…
Henrov
  • 1,610
  • 1
  • 24
  • 52
0
votes
2 answers

End user initiating SQL commands to create a file from a SQL table?

Using SQL Manager ver 18.4 on 2019 servers. Is there an easier way to allow an end user with NO access to anything SQL related to fire off some SQL commands that: 1.)create and update a SQL table 2.)then create a file from that table (csv in my…
mrpush
  • 1
0
votes
1 answer

Why does SQLCMD mode seem to execute xp_cmdshell out of sequence when connecting to other servers?

If I connect to server "ServerA.domain.com" and execute the following in SQLCMD mode of SQL Management Studio: EXEC xp_cmdshell 'hostname' :CONNECT ServerB.domain.com EXEC xp_cmdshell 'hostname' Then the output is: Connecting to…
ir8008
  • 3
  • 2
0
votes
1 answer

trying to understand xp_cmdShell

I know how to use xp_cmdshell to get a listing of a local directory. The statement below gives me a list of cvs files in the dir C:\TEST. I ran the statement in my locally installed SSMS. EXEC Master..xp_cmdShell 'dir C:\TEST\*.csv…
Henrov
  • 1,610
  • 1
  • 24
  • 52