Executing a Query using SQLCMD
-- Server: srv1
-- User: syntaxhelpUser
-- Password: sh@123
-- Database: syntaxhelpDB
-- Query: "SELECT @@version"
sqlcmd -Ssrv1 -UsyntaxhelpUser -Psh@123 -dsyntaxhelpDB -Q"select @@version"
Executing a Query and saving output to a text file
-- Server: srv1
-- User: syntaxhelpUser
-- Password: sh@123
-- Database: syntaxhelpDB
-- Query: "SELECT @@version"
-- Output To: c:\test.txt
sqlcmd -Ssrv1 -UsyntaxhelpUser -Psh@123 -dsyntaxhelpDB -Q"select @@version" -o"c:\test.txt"
Executing a Stored Procedure with SQLCMD
-- Server: srv1
-- User: syntaxhelpUser
-- Password: sh@123
-- Database: syntaxhelpDB
-- SP Name: "GetSalesData"
sqlcmd -Ssrv1 -UsyntaxhelpUser -Psh@123 -dsyntaxhelpDB -Q"EXEC GetSalesData"
Starting a SQL Server Job using SQLCMD
-- Server: srv1
-- User: syntaxhelpUser
-- Password: sh@123
-- Job Name: SendEmail
sqlcmd -Ssrv1 -UsyntaxhelpUser -Psh@123 -dsyntaxhelpDB -Q"EXEC msdb.dbo.sp_start_job @job_name = 'SendEmail'"