I am connecting to sql server which is in another server from my server.
I am executing the following:
Exec master..xp_cmdshell 'bcp "select * from databasename.dbo.tablename "
queryout "D:\tempexportfile.txt" -c -t, -T -S[Connected servername]'
it is writing the text file in the connected server from my server, but i want to the text file is in my server,
Is there any posibility to specify my current server and write text file here?
On the server that you execute the bcp on should connect via a share drive to your server. Then replace the D:\ with the share drive letter.
Another way is to make sure that you have a share on your server that is accessible from the SQL Server, then replace the D:\ with \yourservername\sharename.
Hope that helps.
Related
I have a remote SQL Server with a hostname I am using to connect to.
BCP suggests to use
bcp DBName.dbo.tablename in C:\test\yourfile.txt -c -T -t
However when I try this it does not connect to DBName as that is not a valid alias. I get native error 2.
How to I run BCP but specify an internet / network address to connect to, not an MSSQL server name?
How to I run BCP but specify an internet / network address to connect to, not an MSSQL server name?
You can specify the IP address (here just 127.0.0.1) instead of the server name.
bcp DBName.dbo.tablename in "C:\test\yourfile.txt" -c -T -t -S"127.0.0.1\instance"
If you truly want to go IP\port only then use IP address and port separated by comma
bcp DBName.dbo.tablename in "C:\test\yourfile.txt" -c -T -t -S"127.0.0.1,60905"
Of course in that case you would need to know the port the instance is using, and your instance should be set to use a fixed port
The DBName is the name of the database, you need to use -S option to provide the server (or server\instance) name.
From MSDN:
-S server_name [\instance_name] Specifies the instance of SQL Server to which to connect. If no server is specified, the bcp utility
connects to the default instance of SQL Server on the local computer.
This option is required when a bcp command is run from a remote
computer on the network or a local named instance. To connect to the
default instance of SQL Server on a server, specify only server_name.
To connect to a named instance of SQL Server, specify
server_name\instance_name.
I'm looking to open specific .sql files that have multiple select statements on specific servers at startup in SQL Server Management Studio 2014.
For example, I want to open file 'EmQueries.sql' when I open server sqlemdata9. When I open server sqlplant2, I would like to see file 'PlantQueries.sql' open at startup.
How can I accomplish something like this?
You can achieve the following procedure with Command Line Process, will help you connect to different databases related to specific files and user credentials.
This is Easy with command line (cmd)
ssms "C:\Users\Wizard\Documents\SQL Server Management Studio\query.sql" -S ServerAddressOrIP -d Database -U Username -P Passwordhere
Copy the following line to (.bat) batch file format and run it.
query
EXEC xp_cmdshell 'bcp "select * from DEV.DBO.visit" queryout "c:\users\visit.txt" -c -T'
above works ok and saves output on the remote server;
what i am trying to do here is to save it on my machine that
i launched SQL from
Alex
I had this problem recently. The results of queryout are written on to the database server. A way around this is to use a ftp to transfer the resulting files from the (remote) server on to your local machine.
You can also ask the DBA to provide permissions so the SSMS client can write to your local machine.
When you run xp_cmdshell, you're literally running a command on the SQL Server: the C: drive is the SQL Server's hard drive. To save the output to your machine, your machine will have to have a network share that's visible to the SQL Server. Then you can run the command like this:
EXEC xp_cmdshell 'bcp "select * from DEV.DBO.visit"
queryout "\\my-computer\my-share\users\visit.txt" -c -T'
However, since the command is running under the credentials of the SQL Server, that share also must be writable by the SQL Server account. Which is probably isn't.
The bigger question is, Why are you running bcp on the server instead of running bcp locally?
Open a command shell, and just say:
bcp "select * from dev.dbo.visit"
queryout c:\my-bcp-output-data.txt
-S some-sql-server-instance
-T
I have a bit of an odd requirement to script some MSSQL queries on a remote host.
I need to be able to run these queries via an Enterprise Scheduler, in this case TIDAL 6.1, and save the output of the query as a CSV or XML file.
I'm assuming I would have to set up a local SQL server to act as a bridge to the remote host, and I'm hoping there is a way to tell the script/query to output the results as a file on the local host.
You need to have SQL Client in the machine where you will be running this query. You can run the SQLCMD command below to run the query and save output to file.
sqlcmd -S(servername) -E -i(sql file) -o(output filename) -d(databasename) -s,
-E option is for trusted connection
Hope this helps.
I would to drop remote SQL Server db via sqsh but, I don't know how does it works. I can connect to sql server with command:
sqsh -Ulogin -Ppass -Smssql2005
Once you're connected to the server, you can drop a database with the command
drop database [DBName]
Assuming that there's no one connected to it, it should work. And if it doesn't, it'll tell you why.
echo 'USE table' > script.sqsh
echo 'go' >> script.sqsh
echo 'SELECT * FROM table' >> script.sqsh
echo 'go' >> script.sqsh
sqsh -Ulogin -Ppass -Smssql2005 -i script.sqsh
Maybe you are using the wrong tool to connect to MS SQL Server. If you are looking to connect to MS SQL Server then you can use this code:
SQLCMD -S servername -U username -P password
OR
SQLCMD -S servername -E
The -C argument lets you specify a query to send to the SQL Server instance.
So, I think this will do what you want:
sqsh -Ulogin -Ppass -Smssql2005 -C"DROP DATABASE MyDatabase"