Unblocking The Main Thread Solving ANRs and Frozen Frames
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