Bat File to Delete sql database - sql

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

Related

Run .txt file within ssms SQLCMD

I want to a .txt file that contains a sql script via ssms SQLCMD query window.
Could someone give me an example.
I can run via pc CMD successfully using the following.
sqlcmd.exe -S pcName -E -i "C:\Users\user\Documents\SqlCmdTest.txt"
But how to do the same in SSMS SQLCMD please??
OR a .sql file ??
Regards
Rob
Found Answer in the following post
How can I execute a set of .SQL files from within SSMS?
within SSMS SQLCMD mode
:setvar path "c:\Path_to_scripts"
:r $(path)\file1.sql
:r $(path)\file2.sql

Drop DB but don't delete *.mdf / *.ldf

I am trying to automate a process of detaching and dropping a database (via a VBS objshell.run) If I manually use SSMS to detach and drop I can then copy to database files to another location... however if I use:
sqlcmd -U sa -P MyPassword -S (local) -Q "ALTER DATABASE MyDB set single_user With rollback IMMEDIATE"
then
sqlcmd -U sa -P MyPassword -S (local) -Q "DROP DATABASE MyDB"
It detaches/drops and then deletes the files. How do I get the detach and drop without the delete?
The MSDN Documentation on DROP DATABASE has this to say about dropping the database without deleting the files (under General Remarks):
Dropping a database deletes the database from an instance of SQL Server and deletes the physical disk files used by the database. If the database or any one of its files is offline when it is dropped, the disk files are not deleted. These files can be deleted manually by using Windows Explorer. To remove a database from the current server without deleting the files from the file system, use sp_detach_db.
So in order for you to drop the database without having the files get deleted with sqlcmd you can change it to do this:
sqlcmd -U sa -P MyPassword -S (local) -Q "EXEC sp_detach_db 'MyDB', 'true'"
DISCLAIMER: I have honestly never used sqlcmd before but assuming from the syntax of how it's used I believe this should help you with your problem.
Use SET OFFLINE instead of SET SINGLE_USER
ALTER DATABASE [DonaldTrump] SET OFFLINE WITH ROLLBACK IMMEDIATE; DROP DATABASE [DonaldTrump];
Might it be best to detach the database rather than drop it?
If you drop the database, that implies delete
Note, however, that this will leave your hard disk cluttered with database files you no longer want - in a couple of years time your successor will be running out of space and wondering why the disk is full of MDF files he doesn't recognise

How to Insert million of Insert Command having in .sql file

I have .sql file which contains millions of Insert commands, and they are having insert statements to be inserted into different tables. When I am executing by opening in SQL SERVER MANAGEMENT it says
Insufficient memory to continue the execution of program
You can save the SQL in a file and execute it from the command line using sqlcmd. For example:
sqlcmd -S myServer\instanceName -i C:\myScript.sql
Please note: If your instance is a default instance (i.e. the instance name is MSSQLSERVER), then do not specify it as part of the sqlcmd parameters. To connect to the default instance, simply specify the server name. For example:
sqlcmd -S myServer -i C:\myScript.sql
I would suggest you try running the .sql from the command line (sqlcmd.exe) instead of loading it in SSMS.
SQLCMD - MSDN Link

Batch File Connecting to Server and execute program install on connected server

I have been searching for a resolution for days and I am stuck. I've exhausted all resource and need to resolve this. If any one out there with ideas or know how to do this, please help.
I have an SQL Server Database, on Server1. I have an application, which is installed and runs on Server2 (ServerName2) and a procedure that needs to run within an application that is installed on this server.
What I need is to execute the batch file when ever a row get inserted into a table. The SQL Server database is on Server1. The batch file starts an application and calls a script, which executes a procedure. This script needs to run inside an application which is installed on Server2. I have a trigger after insert created on the table. Now I need to some how execute this batch file. Please note, the batch file calls a program which is installed on a different server.
I thought of 2 solutions:
To use SQL Server to execute the batch file which sits on Server2. I tried using "EXEC xp_cmdshell '\\ServerName2\C:\MSTR SCRIPT FILES\Batch_File_Execute_OTB_Script.bat".
It returned 2 records, the first one "The network name cannot be found." and NULL for second one.
Create a batch file with a connection string to ServerName2, that Start the .exe program. I am able to get the batch file to start this program and run the script if I run this on ServerName2.
In summary, I need to connect from Server1 to Server2, either through SQL Server or through batch? Is this possible? If yes, please provide me with some guidance.
Once I establish a connection, do I need to call the .exe program and run my script? What is the command to trigger this from remote connection?
Here's a little code to help you connect to the SQL server and interrogate the SQL version.
#ECHO OFF
SQLCMD /? > nul 2> nul
IF ERRORLEVEL 1 ECHO.SQLCMD not found on this machine & PAUSE & GOTO :eof
SET SQLCmdLine=SQLCMD -b -w400
REM use something like the next line for NT authentication; second line for SQL authentication
REM for NT SET SQLCmdLine=%SQLCmdLine% -E -S "YourServerName"
REM for SA SET SQLCmdLine=%SQLCmdLine% -U "YourSQLUserName" -P "YourSQLPassword" -S "YourServerName"
SET Database=YourDatabaseName
REM I will use NT Authentication for this example
SET SQLCmdLine=%SQLCmdLine% -E -S "(local)"
SET TestSQLCmdLine=%SQLCmdLine% -h-1 -d "%Database%"
REM this will get the SQL version
%TestSQLCmdLine% -Q "SELECT ##VERSION" | FINDSTR /v "rows affected"
REM this will run a SQL script
%TestSQLCmdLine% -i "YourSQLScript.SQL"
PAUSE

Vaccum full and Reindex Heroku database

I want to perform a full vacuum and reindex on my database for my app hosted on Heroku.
I can't work out how to do it via the heroku command line remotely.
I can do it on my local Mac osx machine via the below commands in terminal...
psql database_name
>> vaccuum full;
>> \q
reindex database database_name
How can i perform a full vaccuum and reindex all my tables for my app on Heroku?
If possible I would like to do it without exporting the database.
Okay so it seems Heroku doesn't support this functionality unless you pay up. Looks like i'll have to pull the database, perform the actions and push it back upstream! Fun times.
You can use the psql interactive terminal with Heroku. From Heroku PostgreSQL:
If you have PostgreSQL installed on your system, you can open a direct psql console to your remote db:
$ heroku pg:psql
Connecting to HEROKU_POSTGRESQL_RED... done
psql (9.1.3, server 9.1.3)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
rd2lk8ev3jt5j50=>
You can also pass-in the parameters at the psql command-line, or from a batch file. The first statements gather necessary details for connecting to your database.
The final prompt asks for the constraint values, which will be used in the WHERE column IN() clause. Remember to single-quote if strings, and separate by comma:
#echo off
echo "Test for Passing Params to PGSQL"
SET server=localhost
SET /P server="Server [%server%]: "
SET database=amedatamodel
SET /P database="Database [%database%]: "
SET port=5432
SET /P port="Port [%port%]: "
SET username=postgres
SET /P username="Username [%username%]: "
"C:\Program Files\PostgreSQL\9.0\bin\psql.exe" -h %server% -U %username% -d %database% -p %port% -e -v -f cleanUp.sql
Now in your SQL code file, add the clean-up SQL, vacuum full (note the spelling). Save this as cleanUp.sql:
VACUUM FULL;
In Windows, save the whole file as a DOS BATch file (.bat), save the cleanUp.sql in the same directory, and launch the batch file. Thanks for Dave Page, of EnterpriseDB, for the original prompted script.
Also Norto, check out my other posting if you want to add parameters to your script, that can be evaluated in the SQL. Please vote it up.