Total Page Preview:   000000056891

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

Lenehan
19-Oct-2021
Hi to every single one, it's in fact a good for me to go to see this web page, it includes helpful Information.
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.

                           
                           

                           

                           

Facebook User: