Execute sql query or sql command sql server using command prompt

22,519 views

Published on

Execute sql query or sql command sql server using command prompt

Published in: Technology
1 Comment
1 Like
Statistics
Notes
  • sqlsplus - great add to SQLCMD

    You can also use free command line tool 'SQLS*Plus' which is like an Oracle SQL*Plus for SQL Server.

    sqlsplus works with SQL Server 2012, 2008, 2005 and 2000 and very flexible with variables (&, &&), data formatting (set lines size, pagesize, etc), etc - much better alternative to sqlcmd or isql

    I found sqlsplus at http://www.sqlsplus.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
22,519
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
71
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Execute sql query or sql command sql server using command prompt

  1. 1. EXECUTE SQL QUERY ORSQL COMMAND SQLSERVER USING COMMANDPROMPTPREPARED BY:IKHWAN KRISNADI – SOFTWARE ENGINEER
  2. 2. USING SQLCMD ONCOMMAND 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. 3. USING SQLCMD ONCOMMAND 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. 4. CONTENT OF script.sqlUSE [WEB_TUTORIAL]GOIF EXISTS (SELECT * FROM sys.objects WHEREobject_id = OBJECT_ID(N[dbo].[MST_MENU]) AND typein (NU))DROP TABLE [dbo].[MST_MENU]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOIF NOT EXISTS (SELECT * FROM sys.objects WHEREobject_id = OBJECT_ID(N[dbo].[MST_MENU]) AND typein (NU))
  5. 5. CONTENT OF script.sql(continue)BEGINCREATE 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]ENDGOSET ANSI_PADDING OFFGO
  6. 6. USING SQLCMD ONCOMMAND PROMPT –Example (continue) Open Command Prompt Type below on Command Prompt:sqlcmd -S IKHWANK-PC,1433MSSQLSERVER -U sa -Pikhwankrisnadi -i C:script.sql -o C:output-script.txt
  7. 7.  After "script.sql" was executed, theres 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
  8. 8. TERIMA KASIH

×