SQL Server - Example for invoking sql file through SQLCMD

SQLCMD utility lets us execute Transact-SQL statements, Stored procedures, and other script files at the command prompt,
in SSMS Query Editor in SQLCMD mode,
in a Windows script file or
in an operating system (Cmd.exe) job step of a SQL Server Agent job.

In this post, let us see an example for invoking sql file through SQLCMD


I have saved the following T-SQL script in path D:\work\SQLQuery1.sql

USE $(database)
GO
SELECT ShiftID,Name FROM [HumanResources].[Shift]


Now goto Start -> Search Programs and files -> type CMD

Form SQLCMD command line like shown below:


sqlcmd -S ServerName -i C:\myScript.sql -U sa -P Password -v database="[Database Name]"




See Also:

No comments: