I have created text file using following command on the server where database resides
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE EXEC xp_cmdshell 'bcp "SELECT top 10 macnum, Cus_name,Cus_Email FROM [VBOS_TSP].[dbo].[Cust_file]" queryout "d:\creatFile.txt" -T -c'
But I need to create that text file on another machine. As I don't want to save that text file on production. Please suggest me other way so that by passing Path and/or passing credentials of a particular server. How to generate that file on other server?
The path in the other machine should be shared path and accessible from this machine and should have full permissions for the folder to read/write.
I worked on similar scenario and below code format works for me. Try it once after give permissions
#LV_FILE_PATH='\\192.168.1.105\Folder\creatFile.txt'
#LV_SQLTXT='"SELECT top 10 macnum, Cus_name,Cus_Email FROM [VBOS_TSP].[dbo].[Cust_file]"'
SET #LV_CMDTXT = 'BCP ' + #LV_SQLTXT + ' QUERYOUT "' + #LV_FILE_PATH + '" -c -U -T -S -r\n'
EXEC MASTER..XP_CMDSHELL #LV_CMDTXT
You can't use an unc path for queryout at bcp. You're only solution is to export it on the current machine and copy it afterwards.
Another idea is to run bcp on the remote maschine and connect to your datastore to export the data by using the parameter -S.
I hope this helps you.
Related
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 /b'
However,
EXEC Master..xp_cmdShell 'dir \\ip-adress\share\folder\*.csv /b'
returns a 'No access'.
But running dir \\ip-adress\share\folder\*.csv /b in my commandshell does return a listing. Apparantly I as a user have sufficient rights. Then why does xp_cmdShell return a 'access denied' ?
Is xp_cmdSHell unusable with shares or has this to do with the account that issues the command? But if I started SSMS locally, should the command not run locally under my WIndowsaccount? Same as in commandshell?
I used a SSIS proxy based on my own credentials but it looks like the machine on which the SQL Server is running, is blocked from accessing that share
So its kind of complicated I think, I've been searching all day for a solution.
I have a SQL Server 2008 R2 and with xp_cmdshell I currently generate some .csv files in C:\
What I want to do now is generate them directly into a linux server in the network.
I tried this :
EXEC xp_cmdshell 'NET USE T: \\192.168.0.25\PERSONS 123456 /user:linuxuser /PERSISTENT:yes'
set #filepath = 'T:'
set #command ='echo '+#customerheader+' > '+#filepath+'\PERSONS\'+convert(varchar,123456)+'.csv'
set #command = replace(#command,'&','e')
exec master..xp_cmdshell #command
EXEC xp_cmdshell 'net use T: /delete'
And what I get is: "The system cannot find the path specified."
While the mapped drive is created succesfully: "The command completed successfully." and "T: was deleted successfully."
Thank you in advance
I think you need to get away from: xp_cmdshell
What about the following?
NET USE T: \\192.168.0.25\PERSONS 123456 /user:linuxuser /PERSISTENT:yes
SET SQLSERVER=YOUR_sql_SERVER
SET SQLQUERY=C:\LOCALFOLDER\somequery.sql
SET OUTPUTFILE=T:\somefolder\someoutput.csv
sqlcmd -S %SQLSERVER% -d YOUR_DATABASE -U SQL_USER -P SQLPASSWORD -i"%SQLQUERY%" -o "%OUTPUTFILE%" -s"," -y30
net use T: /delete
I think the problem is that each time you invoke xp_cmdshell, you invoke a separate PID for CMD.exe and is a different session or instance and is causing you trouble.
Hope this helps!
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 me 1 row affected
and the row is null.
the .exe file is supposed to delete all the content from a certain table.
when i run the file from the windows explorer, the .exe deletes all the content of the above mentioned table, whereas when i try using xp_cmdshell the content of the table remain intact, which means the .exe file is not opened.
any ideas?? this is the code i'm using
exec master..xp_cmdshell 'C:\inetpub\wwwroot\Digital_Library_Shamaa\ShamaaConsoleIndexer\Publish\setup.exe'
i have added the permission Everyone to the folder containing the above .exe file and all its sub folders!
1) Run EXEC master..xp_cmdshell 'whoami' to see which user you're trying to run the exe file
2) Did you enable :
USE master
GO
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE WITH OVERRIDE
GO
3) Did you unblock the file ?
I am able to delete files/folders through the bat file fine, the problem comes when i need to delete old mdf and ldf files.
I get access denied error message.
Is there a way to overcome this in the bat file? without having to open sql managment studio 2008 and delete them there?
Things to note:
At the start I do not specificly know what the database is called, just it's location (c:\sql)
You can use sqlcmd in a batch file to drop the database. Something like this:
sqlcmd -s dbserver -u username -p password -q "DROP DATABASE databasename"
Then you can delete the related mdf and ldf files.
This batch file drops a database even it is being used.
It asks the database name to drop.
#echo off
set /p dbName= "Enter your database name to drop: "
echo Setting to single-user mode
sqlcmd -Q "ALTER DATABASE [%dbName%] SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
echo Dropping...
sqlcmd -Q "drop database %dbName%"
echo Completed.
pause
I am exporting data in SQL Server 2005 with the following command but it produces nothing in SQL server 2008, no file is created although the query is executed succesfully:
execute xp_cmdshell 'bcp "SELECT * FROM MYDB.dbo.MYTABLE" queryout d:\file.csv -c -t; -S(local) -T'
What is wrong with my command?
I managed to execute the command. Solution:
I installed more than one sql server versions. And the management studio cannot find the bcp.exe because in the enviroment path, there are more than one paths for the bcp.exe. You can check this by executing bcp.exe from command prompt and it will say sqlncli.dll was not found.
So you have to put the exact path of bcp.exe to the enviroment path (or in front of other paths). So that windows can use it. Then dont forget to restart the sql server in order to change in paths takes effect. In conclusion, my command is not wrong but the path of bcp.exe is wrong.