SlideShare a Scribd company logo
EXECUTE SQL QUERY OR
SQL COMMAND SQL
SERVER USING COMMAND
PROMPT



PREPARED BY:
IKHWAN KRISNADI – SOFTWARE ENGINEER
USING SQLCMD ON
COMMAND PROMPT
   Command “SQLCMD” on Command
    Prompt to execute SQL Query / SQL
    Command

   Below the format of SQLCMD:
sqlcmd -S [server_name] -U [login_user_name] -P
[login_password] -i
[full_path_of_sql_file_that_will_be_executed] -o
[full_path_of_output_file_after_sql_executed]
USING SQLCMD ON
COMMAND PROMPT –
Example
 I have a file that contains sql script.
  The file named “script.sql” and it is on
  root of drive C
 Below the content of script.sql:
CONTENT OF script.sql
USE [WEB_TUTORIAL]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE
object_id = OBJECT_ID(N'[dbo].[MST_MENU]') AND type
in (N'U'))
DROP TABLE [dbo].[MST_MENU]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE
object_id = OBJECT_ID(N'[dbo].[MST_MENU]') AND type
in (N'U'))
CONTENT OF script.sql
(continue)
BEGIN
CREATE TABLE [dbo].[MST_MENU](
          [MENU_ID] [varchar](17) NOT NULL,
          [MENU_NAME] [varchar](250) NOT NULL,
          [MENU_PATH] [varchar](2500) NOT NULL,
          [PARENT_ID] [varchar](17) NULL,
          [IS_ACTIVE] [int] NOT NULL,
          [CREATED_BY] [varchar](250) NOT NULL,
          [CREATED_DATE] [datetime] NOT NULL,
          [UPDATED_BY] [varchar](250) NOT NULL,
          [UPDATED_DATE] [datetime] NOT NULL,
CONSTRAINT [PK_MST_MENU] PRIMARY KEY CLUSTERED
(
          [MENU_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING OFF
GO
USING SQLCMD ON
COMMAND PROMPT –
Example (continue)
 Open Command Prompt
 Type below on Command Prompt:
sqlcmd -S IKHWANK-PC,1433MSSQLSERVER -U sa -P
ikhwankrisnadi -i C:script.sql -o C:output-script.txt
   After "script.sql" was executed, there's
    a new file with named "output-
    script.txt“ on root of drive C

   Open “output-script.txt” to view result

   Open database to view changes after
    “script.sql” was executed
TERIMA KASIH

More Related Content

PPTX
Beginner guide to mysql command line
Priti Solanki
 
PPT
Raj mysql
firstplanet
 
PDF
Mysql administration
beben benzy
 
PDF
How to export import a mysql database via ssh in aws lightsail wordpress rizw...
AlexRobert25
 
DOCX
Materi my sql part 1
Amar Senjaku Ofdetraisar
 
TXT
CONTOH MEMBUAT TABEL XAMPP
Bina Sarana Informatika
 
PDF
New features in Performance Schema 5.7 in action
Sveta Smirnova
 
DOCX
Tugas praktikum smbd
Amar Senjaku Ofdetraisar
 
Beginner guide to mysql command line
Priti Solanki
 
Raj mysql
firstplanet
 
Mysql administration
beben benzy
 
How to export import a mysql database via ssh in aws lightsail wordpress rizw...
AlexRobert25
 
Materi my sql part 1
Amar Senjaku Ofdetraisar
 
CONTOH MEMBUAT TABEL XAMPP
Bina Sarana Informatika
 
New features in Performance Schema 5.7 in action
Sveta Smirnova
 
Tugas praktikum smbd
Amar Senjaku Ofdetraisar
 

What's hot (20)

PPTX
Introduction to PostgreSQL
Joel Brewer
 
PPTX
Introduction databases and MYSQL
Naeem Junejo
 
ODP
Beyond PHP - it's not (just) about the code
Wim Godden
 
PDF
Firebird
Chinsan Huang
 
ODP
Nginx and friends - putting a turbo button on your site
Wim Godden
 
PDF
Flexviews materialized views for my sql
Justin Swanhart
 
PDF
Preparse Query Rewrite Plugins
Sveta Smirnova
 
PPTX
Database administration commands
Varsha Ajith
 
PDF
MySQL5.7で遊んでみよう
yoku0825
 
PDF
Noinject
Justin Swanhart
 
PDF
How To Install and Configure GNome on CentOS 7
VCP Muthukrishna
 
PDF
How To Check and Delete a File via PowerShell
VCP Muthukrishna
 
TXT
Oracle ORA Errors
Manish Mudhliyar
 
PPTX
利用Init connect做mysql clients stat 用户审计
Dehua Yang
 
PDF
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
VCP Muthukrishna
 
PDF
Data Processing Inside PostgreSQL
EDB
 
PDF
How To Manage Linux User on RHEL 7
VCP Muthukrishna
 
ODP
Msql
ksujitha
 
PPTX
Php mysq
prasanna pabba
 
PDF
MySQL server security
Damien Seguy
 
Introduction to PostgreSQL
Joel Brewer
 
Introduction databases and MYSQL
Naeem Junejo
 
Beyond PHP - it's not (just) about the code
Wim Godden
 
Firebird
Chinsan Huang
 
Nginx and friends - putting a turbo button on your site
Wim Godden
 
Flexviews materialized views for my sql
Justin Swanhart
 
Preparse Query Rewrite Plugins
Sveta Smirnova
 
Database administration commands
Varsha Ajith
 
MySQL5.7で遊んでみよう
yoku0825
 
Noinject
Justin Swanhart
 
How To Install and Configure GNome on CentOS 7
VCP Muthukrishna
 
How To Check and Delete a File via PowerShell
VCP Muthukrishna
 
Oracle ORA Errors
Manish Mudhliyar
 
利用Init connect做mysql clients stat 用户审计
Dehua Yang
 
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
VCP Muthukrishna
 
Data Processing Inside PostgreSQL
EDB
 
How To Manage Linux User on RHEL 7
VCP Muthukrishna
 
Msql
ksujitha
 
Php mysq
prasanna pabba
 
MySQL server security
Damien Seguy
 
Ad

Viewers also liked (17)

PPS
Execute MySQL query using command prompt
Ikhwan Krisnadi
 
PPTX
SQLcl overview - A new Command Line Interface for Oracle Database
Jeff Smith
 
PDF
Introduction to MySQL
Giuseppe Maxia
 
PPT
Sql ppt
Anuja Lad
 
PPTX
Chương 4: MySQL
Hồ Lợi
 
PDF
[Thực tập][GameLoft] Lập trình game đa nền tảng trên di động
Trung Hiếu Trần
 
PPT
Executing Sql Commands
phanleson
 
PDF
Tool Development 08 - Windows Command Prompt
Nick Pruehs
 
PPTX
Introduction to Powershell Version 5
Nishtha Kesarwani
 
PPTX
Creating database using sql commands
Belle Wx
 
PDF
Laravel 5 framework
Trung Hiếu Trần
 
PDF
Windows command prompt a to z
Subuh Kurniawan
 
ODP
Php variables (english)
Mahmoud Masih Tehrani
 
PPTX
Constructor and encapsulation in php
SHIVANI SONI
 
PPTX
OOPS Characteristics (With Examples in PHP)
baabtra.com - No. 1 supplier of quality freshers
 
PPT
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
 
Execute MySQL query using command prompt
Ikhwan Krisnadi
 
SQLcl overview - A new Command Line Interface for Oracle Database
Jeff Smith
 
Introduction to MySQL
Giuseppe Maxia
 
Sql ppt
Anuja Lad
 
Chương 4: MySQL
Hồ Lợi
 
[Thực tập][GameLoft] Lập trình game đa nền tảng trên di động
Trung Hiếu Trần
 
Executing Sql Commands
phanleson
 
Tool Development 08 - Windows Command Prompt
Nick Pruehs
 
Introduction to Powershell Version 5
Nishtha Kesarwani
 
Creating database using sql commands
Belle Wx
 
Laravel 5 framework
Trung Hiếu Trần
 
Windows command prompt a to z
Subuh Kurniawan
 
Php variables (english)
Mahmoud Masih Tehrani
 
Constructor and encapsulation in php
SHIVANI SONI
 
OOPS Characteristics (With Examples in PHP)
baabtra.com - No. 1 supplier of quality freshers
 
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
 
Ad

Recently uploaded (20)

PDF
This slide provides an overview Technology
mineshkharadi333
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
 
PDF
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
PDF
GYTPOL If You Give a Hacker a Host
linda296484
 
This slide provides an overview Technology
mineshkharadi333
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Software Development Company | KodekX
KodekX
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ
 
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Doc9.....................................
SofiaCollazos
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
Software Development Methodologies in 2025
KodekX
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
GYTPOL If You Give a Hacker a Host
linda296484
 

Execute sql query or sql command sql server using command prompt

  • 1. EXECUTE SQL QUERY OR SQL COMMAND SQL SERVER USING COMMAND PROMPT PREPARED BY: IKHWAN KRISNADI – SOFTWARE ENGINEER
  • 2. USING SQLCMD ON COMMAND PROMPT  Command “SQLCMD” on Command Prompt to execute SQL Query / SQL Command  Below the format of SQLCMD: sqlcmd -S [server_name] -U [login_user_name] -P [login_password] -i [full_path_of_sql_file_that_will_be_executed] -o [full_path_of_output_file_after_sql_executed]
  • 3. USING SQLCMD ON COMMAND PROMPT – Example  I have a file that contains sql script. The file named “script.sql” and it is on root of drive C  Below the content of script.sql:
  • 4. CONTENT OF script.sql USE [WEB_TUTORIAL] GO IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MST_MENU]') AND type in (N'U')) DROP TABLE [dbo].[MST_MENU] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MST_MENU]') AND type in (N'U'))
  • 5. CONTENT OF script.sql (continue) BEGIN CREATE TABLE [dbo].[MST_MENU]( [MENU_ID] [varchar](17) NOT NULL, [MENU_NAME] [varchar](250) NOT NULL, [MENU_PATH] [varchar](2500) NOT NULL, [PARENT_ID] [varchar](17) NULL, [IS_ACTIVE] [int] NOT NULL, [CREATED_BY] [varchar](250) NOT NULL, [CREATED_DATE] [datetime] NOT NULL, [UPDATED_BY] [varchar](250) NOT NULL, [UPDATED_DATE] [datetime] NOT NULL, CONSTRAINT [PK_MST_MENU] PRIMARY KEY CLUSTERED ( [MENU_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] END GO SET ANSI_PADDING OFF GO
  • 6. USING SQLCMD ON COMMAND PROMPT – Example (continue)  Open Command Prompt  Type below on Command Prompt: sqlcmd -S IKHWANK-PC,1433MSSQLSERVER -U sa -P ikhwankrisnadi -i C:script.sql -o C:output-script.txt
  • 7. After "script.sql" was executed, there's a new file with named "output- script.txt“ on root of drive C  Open “output-script.txt” to view result  Open database to view changes after “script.sql” was executed