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"
Related
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 can successfully connect to my local server from SQL Management Studio, but when I try to connect from sqlcmd, there is an error: "Login failed for user 'my_username'".
I have checked my username and password - I can login with them in SQL Management Studio.
P.S.
I think that there is a problem in configuration of MS SQL, cause I can't connect to local server with "(local)" name and allways connect to it with COMP\COMP (COMP is my computer name). How can I correct it?
sqlcmd works when I connect to my team network server
I was having same problem, in the comment there is the solution by #Dave K
sqlcmd -S (local)\COMP -U user_name -P password
or
sqlcmd -S .\COMP -U user_name -P password
Hope it helps someone
From the system where the server is running, I can login using the following command:
sqlcmd -S "ComputerName\InstanceName" -d "DatabaseName" -i "sql.txt" -s"," -o "result.csv"
I want to connect to this server from another machine, So I tried this
sqlcmd -U "UserName" -S "ComputerName\InstanceName" -d "DatabaseName" -i "sql.txt" -s"," -o "result.csv"
"UserName" above is the default user name of the server machine that has administrative privileges. This user account does not have any password.
After issuing that command, it asks for password. Since there is no password, I hit enter. But I get the following error:
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Login failed for user 'UserName'..
SQLCMD Reference
i met this situation:
sqlcmd -S "server" -d "db" -E -i test.sql
which gives "Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Login failed for user 'DOMAIN\administrator'.."
but when i switched to:
sqlcmd -S server -d db -E -i test.sql
it works fine. so look out for your double quotes. hope this maybe of help.
-U UserName specifies the name of a SQL Server login, not a Windows user. It seems to me you want to use a Windows user. The proper way to do it is to have a domain, run as a domain user and grant appropriate permissions in SQL to domain group of which you are member. Bare that, you can use runas /netonly /user:ComputerName\UserName sqlcmd -E.
The issue is related to differences between SQL and windows authentication.
In case of windows authentication e.g. in domain environment with several untrusted domains it is only option to use runas.exe /netonly /user:YourTargetDomain\SQLUser to run SQLCMD or Ssms.exe (SQL management studio) under the proper SQL User.
Both sqlcmd and ssms can use Windows (also domain) authentication without any credentials SQLCMD -E (UI option in Studio) but process should be run as a local or domain user configured for NT Authentification in SQL. Or provide SQL user credentials (configured directly on SQL server e.g. SA) - SQLCMD -U SQLUserName -P SQLUserPassword (the second UI option in the Studio).
Commands below can be used to connect to SQL server configured with Windows (NT) authentication (e.g. lab domain user) from machine in separate untrusted domain (e.g. from office laptop with your office domain account - not configured to access the SQL server).
To run SSMS (it is possible to create a windows desktop shortcut, path to ssms can be different, depends on used version and installation preferences):
C:\Windows\System32\runas.exe /netonly /user:MyLabDomain\SqlUser "C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\Ssms.exe
To run a query with SQLCMD (in the example, it cleans a table, also can be used as a shortcut):
C:\Windows\System32\runas.exe /netonly /user:MyLabDomain\SqlUser "sqlcmd -S 123.123.12.34 -E -I -Q \"delete FROM TestDB..TestTableToEmpty\""
SSMS or SQLCMD should be installed on machine from which you connect to SQL server.
After run you should enter passwords for the SQL User.
I suppose, these examples may help somebody.
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.
I can connect to my SQL Server database via sqlcmd from a DOS command window, but not from a Cygwin window. From DOS:
F:\Cygnus>sqlcmd -Q "select 'a test'" -S .\SQLEXPRESS
a test
(1 rows affected)
F:\Cygnus>
====================================================
From Cygwin:
$ sqlcmd -Q "select 'a test'" -S .\SQLEXPRESS
HResult 0x35, Level 16, State 1 Named Pipes Provider: Could not
open a connection to SQL Server [53]. Sqlcmd: Error: Microsoft SQL
Native Client : An error has occurred while establishing a connection
to the server. When connecting to SQL Server 2005, this failure may be
caused by the fact that under the default settings SQL Server does not
allow remote connections.. Sqlcmd: Error: Microsoft SQL Native Client
: Login timeout expired.
The backslash is being eaten by cygwin's bash shell. Try doubling it:
sqlcmd -Q "select 'a test'" -S .\\SQLEXPRESS
You may have to allow remote connections for this, and give the full server name i.e SERVER\SQLEXPRESS
You can also pass query/instruction to db and receive output in shell if you use "-Q" switch:
sqlcmd -Q "select * from nice.dbo.TableName ac ORDER BY 1 DESC" -S server_name\\db_name