Total Page Preview:   000000002932

How to run or execute SQL Script file using CMD or Command line

In this article i am going to expain how to run ro execute SQL Script file using CMD(Command line). Using the command line we can run or execute any SQL scripts file with SQL Server Management Studio(SSMS) step to step
 
Step 1: Please below my Example, copy the following code and and paste into the notepad it as  save  Product.sql file under the C:\SQLScripts\ folder. (First you should create folder SQLScripts in C Drive if you not created)
 
USE [master]
GO
------------- Check database if already exists then drop -------------------
IF EXISTS (SELECT [name] FROM sys.databases WHERE [name] = 'db_Product' )
     DROP DATABASE db_Product
GO
 
------------- Create Database-------------
CREATE DATABASE db_Product
GO
 
-------------Switch database -------------
USE [db_Product]
GO
 
------------ Check table if already exists then drop -------------------
IF EXISTS (SELECT [name] FROM sys.tables WHERE [name] = 'tbl_Product' )
    DROP TABLE tbl_Product
GO
 
------------ Create tbl_Product table ----------------------------------
CREATE TABLE tbl_Product
(
      ProductId INT,
      ProductName VARCHAR(50),
      ProductPrice FLOAT,
      Quintity INT,
)
GO
 
----------------Insert data into tbl_Product table-------------------------
INSERT INTO tbl_Product VALUES (1, 'Mobile', 5200, 15);
INSERT INTO tbl_Product VALUES (2, 'Laptop', 25452, 25);
INSERT INTO tbl_Product VALUES (3, 'Desktop', 18451, 8);
 
----------------Count total inseted data --------------------------------
SELECT COUNT(*) AS 'TotalRecords' FROM tbl_Product
 
 
Once you have save successfully Product.sql in the SQLScripts folder then go to next step.
 
Step 2: Using the CMDSQL Command:
 
    1: Open the run window and type CMD then enter.
    2: Type command CD C:\  for Change the directory to C drive
    3: Type command CD "SQLScripts" for select SQLScripts folder where your saved SQL scripts file.
    4: Using dir command you can see Product.sql file.
 
 

 

Step 3: In this step we will connect to your local sql server database using SQLCMD. Through this same process you may connect to any remote sql server on your network.
 
  SQLCMD -S HSS-457\SQLSERVER2014 -E -i Product.sql
  
  The -S Server Name (<ComputerName>\<SQLServerInstanceName>) Example : HSS-457\SQLSERVER2014
  The -E switch is used for trusted connection.
  The -i switch is for .sql script file name.
 
 
 
Now you are able to see Database, table and data which you have created using command line using SQL Server management Studio(SSMS):
 
 
 
Thanks!
By Brijesh Kumar
 

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments


                           
                           

                           

                           

Facebook User: