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

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
 

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)

PPTX
CapCut Pro PC Crack Latest Version Free Free
josanj305
 
PPTX
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
PDF
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PDF
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PPTX
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
PDF
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
CapCut Pro PC Crack Latest Version Free Free
josanj305
 
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
Kubernetes - Architecture & Components.pdf
geethak285
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
Simplify Your FME Flow Setup: Fault-Tolerant Deployment Made Easy with Packer...
Safe Software
 
Ad

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