34

I am using a SQL Server database. I have these SQL queries:

Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;

Is it possible to run these scripts using a batch file? The database is a remote database.

Thanks!

2
  • 1
    Open a cmd prompt and type sqlcmd /? Commented Feb 4, 2014 at 13:40
  • Nagaraj, it was for oracle. Now the question for sqlserver. Commented Feb 4, 2014 at 13:41

4 Answers 4

68

Save the commands in a .SQL file, ex: ClearTables.sql, say in your C:\temp folder.

Contents of C:\Temp\ClearTables.sql

Delete from TableA;
Delete from TableB;
Delete from TableC;
Delete from TableD;
Delete from TableE;

Then use sqlcmd to execute it as follows. Since you said the database is remote, use the following syntax (after updating for your server and database instance name).

sqlcmd -S <ComputerName>\<InstanceName> -i C:\Temp\ClearTables.sql

For example, if your remote computer name is SQLSVRBOSTON1 and Database instance name is MyDB1, then the command would be.

sqlcmd -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql

Also note that -E specifies default authentication. If you have a user name and password to connect, use -U and -P switches.

You will execute all this by opening a CMD command window.

Using a Batch File.

If you want to save it in a batch file and double-click to run it, do it as follows.

Create, and save the ClearTables.bat like so.

echo off
sqlcmd -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql
set /p delExit=Press the ENTER key to exit...:

Then double-click it to run it. It will execute the commands and wait until you press a key to exit, so you can see the command output.

Sign up to request clarification or add additional context in comments.

8 Comments

+1 The question says "The database is remote database."... can you adjust the command line?
Shiva, Thanks much for your answer. Can i place -E -S SQLSVRBOSTON1\MyDB1 -i C:\Temp\ClearTables.sql in batch file and just double click on it ?
Yes. Save it as a .BAT file. You might want to put a pause at the next line so that the CMD window doesn't run the delete and exit, and you are able to see the output before closing the window. I updated my answer for this.
Shiva, can i use same sqlcmd to run the queries against oracle db?
|
5

Check out SQLCMD command line tool that comes with SQL Server. http://technet.microsoft.com/en-us/library/ms162773.aspx

6 Comments

Bate Tech, i have to run the batch file in the machine in which no sql server is installed. I have to run it on remote database. Database is not there in the machine in which i am executing the batch file.
Use the :connect statement. This also can be done in SSMS using the SQLCMD window type.
@user755806: You can use sqlcmd's -S parameter to choose the server
Use powershell below. It is installed on every Windows 7 machine and above or install the SSMS management pack.
@user755806 I updated my answer for remote database. Check it out.
|
2

Use the SQLCMD utility.

http://technet.microsoft.com/en-us/library/ms162773.aspx

There is a connect statement that allows you to swing from database server A to server B in the same batch.

:Connect server_name[\instance_name] [-l timeout] [-U user_name [-P password]] Connects to an instance of SQL Server. Also closes the current connection.

On the other hand, if you are familiar with PowerShell, you can programmatic do the same.

http://technet.microsoft.com/en-us/library/cc281954(v=sql.105).aspx

Comments

0

Different ways:

  1. Using SQL Server Agent (If local instance)
    schedule a job in sql server agent with a new step having type as "T-SQL" then run the job.

  2. Using SQLCMD
    To use SQLCMD refer http://technet.microsoft.com/en-us/library/ms162773.aspx

  3. Using SQLPS
    To use SQLPS refer http://technet.microsoft.com/en-us/library/cc280450.aspx

3 Comments

Using SQL Server Agent will not help. The database is remote. You will need a linked server in addition.
@CRAFTYDBA That's correct (will update the answer). but a possible way to run scripts in local instance.
Looks like the SQLPS link is being deprecated and redirects to the new version of SQLPS Module learn.microsoft.com/en-us/sql/powershell/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.