Drop DB using MSBuild even if in use - sql-server

I am using the following command in my MSBuild file to drop a database
sqlcmd -E -S <ServerName> -Q "DROP DATABASE <DBName>"
But sometimes I get the error
Cannot drop database because it is currently in use.
What command should I use so that the DB is dropped even if it is in use?

You can set your database to SINGLE_USER to drop all existing connections.
sqlcmd -E -S <ServerName> -Q "ALTER DATABASE <DBName> SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
sqlcmd -E -S <ServerName> -Q "DROP DATABASE <DBName>"
This is preferred to looping over each of the connections and dropping them one by one, because some applications will immediately reconnect.

To kill all connections to your database. See Kill All Active Connections To A Database
Now issue your DROP DATABASE command after the above procedure.

Please use something like this:
sqlcmd -S servername\instance -Q"use master;DROP DATABASE yourdatabase"

Try this:
sqlcmd -S .\MAPS -Q "RESTORE DATABASE Awards FROM DISK = 'C:\Awards_Project
\Awards_FULL.bak' WITH REPLACE, MOVE 'Awards' to 'C:\Awards_Project\Awards.mdf',
MOVE 'Awards_log' to 'C:\Awards_Project\Awards.ldf'"

You could alternatively restart the sql service and then perform the drop e.g. in a myfile.bat
#echo off
REM Requires administrative rights to restart service
REM open powershell and execute command in administrative mode
powershell -Command "Start-Process 'cmd' -Verb RunAs -ArgumentList '/c net stop MSSQLSERVER && net start MSSQLSERVER && exit'"
echo Please wait ~10 seconds for sql service to restart.
Timeout /t 10 /nobreak
echo Dropping database.
sqlcmd -S localhost -Q "drop database [database Name]"

You cannot drop a database that is in use. Check link for ref

Related

Why database creation command fails in SQLCMD in SQL Server docker for linux?

I use docker-compose.yml to load my SQL Server image inside a container.
After it's up and running, I create a command.sh shell and try to run it to create a database.
# command.sh
echo 'creating database from ->' $ModuleName
export query="'create database $ModuleName'"
echo $query
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P my_strong_password -Q $query
And it gives me this error:
Sqlcmd: 'database': Unknown Option. Enter '-?' for help.
Please note that I can't use -i switch to use an input .sql file, because I'm creating my queries programmatically in shell based on environment variables.
The output of sqlcmd -? shows how to use then -Q option. On windows this says [-Q "cmdline query" and exit].
But Windows and Linux differ (or are not consistent) in the use of single- or double quotes.
First option is to try: sqlcmd -Q "\"create database $ModuleName\""
Second option is:
Create a temporary file (i.e. /tmp/tmp.sql), and put the SQL statement in that script.
Use -i /tmp/tmp.sql to execute that script.

How to skip invalid servers in SQLCMD mode in SSMS

I am running sqlcmode mode in ssms agains multiple servers:
:CONNECT SERVER1
script
GO
:CONNECT SERVER2
script
GO
:CONNECT SERVER3
script
GO
etc, however when server2 is not accessible it stops there and won't connect to server3, is there a way to ignore connection errors in SQLCMD mode?
:CONNECT related exceptions stop script execution.
As a possible workaround for SQLCMD mode:
!!sqlcmd -SServerA -Q"select 1"
!!sqlcmd -SServerB -Q"select 2"
!!sqlcmd -SServerC -Q"select 3"
Update: removed :on error ignore

SQL Server Express backup script

I was able to perform backup for my SQL Server Express using this command at the command prompt:
SQLCMD -E -S testing\SQLEXPRESS –Q "BACKUP DATABASE testing3 TO DISK='C:\Users\Administrator\Desktop\test.bak'"
When I want to create a batch script using the command above in order to create a task scheduler to perform auto backup, it shows error as below:
Here is the batch code:
echo off
c: \
CD "C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn"
SQLCMD -E -S testing\SQLEXPRESS –Q "BACKUP DATABASE testing3 TO DISK='C:\Users\Administrator\Desktop\test.bak';"
I didn't put exit at bottom of it because I want to verify whether the command successfully run or not.
ERROR:
Sqlcmd: 'ûQ "BACKUP DATABASE testing3 TO DISK='C:\Users\Administrator\Desktop\test.bak' ': Unexpected argument. Enter '-?' for help.
Does anyone know how to fix this?
EDIT : I solve it by giving a semi column at the end and type it one by one instead of copy paste it

how to restore Sql server backup using batch file

Below is my batch file which is taking backup but showing an exception "unexcepted argument" on execution of restore command. Pl help me solving this issue, TIA.
One more doubt can I take backup of remote server on my local folder, here I am taking backup on remote machine itself.
set SqlServer=.
set InstanceName=MSSQLSERVER
set Username=xxx
set Password=xxxx
set Database=abc
set LocalFolder=C:\db_backups
SqlCmd -S %SqlServer% -U %Username% -P %Password% -Q "Backup Database %Database% To Disk='%LocalFolder%\%Database%-%CurrentDate%.bak'"
SqlCmd -S %SqlServer% -U %Username% -P %Password% –Q "RESTORE DATABASE [test] FROM DISK='%LocalFolder%\%Database%-%CurrentDate%.bak'"

sqlcmd using windows authentication in automation script

I am using below command in bat file to get some information from the list of sql instance,i am using -E to login as windows authentication when i manually triggering the bat file.
Could anyone help how to configure it on SQLJOB.i am sure that - E will not work on SQLjob ..
Thanks in advance!!
FOR /F %%G IN (E:\serverlist\serverlist.txt) DO (echo %%G
sqlcmd -S %%G -iE:\serverlist\some.sql -h-1 -W -w1000 -s "," -m1 -E >>E:\output\result.txt
)
using proxy account helped to resolve the issue.just add the sqlcmd command under a job step with Run as 'Proxy account'
try this.
sqlcmd -S .\sqlexpress -E -i "c:\test.sql" -o "c:\output.txt"
(This command would execute a SQL script on a local SQLExpress server using Windows Authentication)
sqlcmd -S mysql -U sqluser -P sqlpassword -i "c:\test.sql" -o "c:\output.txt"
(This command would execute a SQL script on a SQL server using SQL authentication)

Resources