SQL 2016 Hosting UK – How to Execute SQL Server Scripts From Command Line With Append Output File

I know you feel SSMS is really simple yet in the event that you have 1000 MSSQL Script to be sent ? Do you still prefer SSMS ? So to avoid from squandering time most ideal path is to run scripts utilizing command prompt or batch file.

And here is the code:

In “Drive:\windows\mydatabase.sql” is sql script having code “Select @@servername”

1. Syntax: sqlcmd -S Server\Instance -U user -P password -i path\file.sql

Example: sqlcmd -S Windows-PC\SQL2014 -U mydatabase -P mydatabase -i C:\windows\cmd.sql

2. If you want Windows authentication you should open Command Prompt with “Run as Administrator”

Syntax:sqlcmd -E -S Servername\instance -i Drive:\folderpath\cmd.sql
/**** Query output of sql server from command prompt in flat file ****/

Here using “>;” will clear output file and write data into it

sqlcmd -S Windows-PC\SQL2014 -U mydatabase -P mydatabase -i C:\windows\cmd.sql > C:\Windows\out.txt

sqlcmd -E -S Windows-PC\SQL2014 -i C:\windows\cmd.sql C:\windows\out.txt

Here using “>>” will append output file
sqlcmd -S Windows-PC\SQL2014 -U mydatabase -P mydatabase -i C:\windows\cmd.sql >> C:\windows\out.txt

sqlcmd -E -S Windows-PC\SQL2014 -i C:\windows\cmd.sql >> C:\windows\out.txt

HostForLIFE.eu SQL Server 2016 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. They deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. They have customers from around the globe, spread across every continent. They serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.