Total Page Preview:   000000031629

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

Sweet
09-Apr-2021
You made some decent points there. I looked on the web for more information about the issue and found most individuals will go along with your views on this web site. 0mniartist asmr
Day
22-Jul-2021
Superb, what a website it is! This web site gives valuable data to us, keep it up.
Vardon
06-Aug-2021
Podsumowująϲ, cennik pozycjonowania jest z pewnośⅽią istotną informacją, którą potencjalny klient chciałƄy poznać, jednak nie jest tо czynnik determinujący ostateczną decyzję ߋ promowaniu swojego biznesu.
Monroy
18-Jul-2021
Thanks for sharing your thoughts about How to run or execute SQL Script file using CMD or Command line. Regards
mac
16-Jun-2021
This is really interesting post, You are a very skilled blogger.I have joined your feed and look forward to seeking more of your fantastic post.The problem is something which not enough folks are speaking intelligently about. Now i’m very happy that I came across this in my search for something concerning this.Also, I have shared your site in my social networks! macbook pro stuck on apple logo screen | stuck at white screen apple logo macbook | macbook pro stuck on loading screen | mac stuck on loading screen with apple https://www.mygeekshelp.com/macbook-stuck-on-apple-logo-screen/
Daecher
21-Jul-2021
Aѡ, this was an incredibly nice post. Finding the time and actual effort to create a top notch ɑrticle… but what can I ѕay… I рrocrastinatе a lot and never sеem tօ ցet anything done.

                           
                           

                           

                           

Recent Posts

How to get the alternate rows or records from table in sql server
07-Jun-2017
Data Warehouse Three Tier Architecture
20-Aug-2017
SQL Server - Fragmentation on azure sql database
07-Oct-2018
Limitations of Azure SQL Database
19-Sep-2019
PIVOT and UNPIVOT in SQL Server with Example
29-Jun-2017
SQL Server - Find all index with key columns on Azure SQL Database
02-Oct-2018
SQL Server - Find unused indexes on azure SQL database scripts
02-Oct-2018
Find Resource usage in Azure SQL Database using script or T-SQL
07-Oct-2018
How to Set client IP Address using the firewall security in Azure SQL Using Portal
29-Jun-2017
What is a Trigger and type of trigger in SQL server?
10-Sep-2021
What is Table Variable? Explain its advantages and disadvantages?
10-Sep-2021
Top 60 SQL Server Interview Questions Answers for freshers and experienced
17-Sep-2021
Cloud Data Engineer SQL Server Interview Questions Answers for freshers and experienced
31-Aug-2021
Top 16 performance query optimization techniques in SQL Database?
16-Sep-2021
Data warehouse Interview Questions Answers for freshers and experienced
16-Sep-2021
What are the types of User Define Function and difference between a nested UDF and recursive UDF in SQL server?
10-Sep-2021
What are the different types of Error Handling in SQL Server?
11-Sep-2021
What are the differences between OLTP and OLAP?
07-Sep-2021
How many types of dimension tables available in data warehouse?
16-Sep-2021
What are characteristics and advantages of stored procedure?
10-Sep-2021

Facebook User: