Tuesday, December 30, 2014

Execute SQL Script Using SQLCMD Command Line in SQL Server


Large script files not execute in SQL Server Management Studio.

so we can execute it Using SQLCMD Command Line.

SQLCMD command line Utility is an alternative way to execute SQL scripts using the command line. 
This allows us to execute SQL scripts without touching SQL Server Management Studio.


1. Click Start button , 
2. open Run command and write below query in to Run Command and Click ok button.

3. sqlcmd -S Servername -i C:\Scriptname.sql
4. after write query into Run Command, 
   Open Sql Command Prompt and Execute query in to Command Prompt.



ex.

below code save it as Scriptname.sql file under the C:\Scriptname.sql

USE Master
GO

IF EXISTS ( SELECT [name] FROM sys.databases WHERE [name] = 'DBEmployee' )
DROP DATABASE DBCompany
GO

CREATE DATABASE DBEmployee 
GO

USE DBEmployee  
GO

IF EXISTS ( SELECT [name] FROM sys.tables where [name]='EmployeeMaster' )
DROP TABLE Employee
GO

CREATE TABLE EmployeeMaster
(
EmpId int,
Fname varchar(40),
Lname varchar(40),
Hiredate datetime,
Salary int
)
GO

INSERT INTO EmployeeMasterVALUES (1,'Akash','Patel','12/31/2014',5000);



I have to get whole database script 983 MB size, executed script approx 2 hrs.  

No comments:

Post a Comment