MS-SQL: Run a SQL Script on Database Server

by on March 6, 2008 · 5 comments· LAST UPDATED December 20, 2008

in , ,

Q. How do I execute a SQL script (.sql file) on my MS-SQL database server under Windows Sever 2003?

A. You can easily run any .sql file on MS-SQL database server using the Query Analyzer tool.

How can I execute a SQL script (.sql file) on my SQL Server ?

Open the Query Analyzer by visiting Start → Programs → MS SQL Server → Query Analyzer

Once opened, connect to the database that you are wish running the script on.

Next, open the SQL file using File → Open option. Select .sql file.

Once it is open, you can execute the file by pressing F5.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 5 comments… read them below or add one }

1 Faris Zuriekat, M.S January 30, 2009 at 11:58 pm

H for host ,d for database , U for user and P for password

EXEC master.dbo.xp_cmdshell ‘isql -Hhostname -ddbname -Uusername -Ppassword -i file’

example
EXEC master.dbo.xp_cmdshell ‘isql -H10.203.420.155 -dmdd3 -Umed -Piddmapis -i c:\upgrade110490.sql’

Reply

2 Steve Atkinson March 22, 2012 at 2:51 pm

Just visited the MS SQL ‘How Do I’ site… Just read this article… the burning question is: how do I persuade MS that they need to rethink their product from the bottom up, and present it as a clone of ORACLE so there is a chance of using some flexibility in scripting, procedures, packages etc…

Reply

3 Dinesh vishe June 20, 2012 at 10:28 am

If i am using windows autheication mode then how this appera…

EXEC master.dbo.xp_cmdshell ‘isql -Hhostname -ddbname -Uusername -Ppassword -i file’ ????

Reply

4 Harshita August 14, 2012 at 8:42 am

Thankss for the info..

Reply

5 Sudarshan August 24, 2012 at 6:02 am

@Farhad, @Dinesh
the solution using EXEC master.dbo.xp_cmdshell .. is actually generating a command shell!

A lighter way to do this would be to enable SQLCMDmode using the toolbar button, and then run the command as follows
:R C:\somefolder\mysql.sql

if you like the output to be captured into a text file, run the :out beforehand
:out c:\somefolder\myoutputfile.lst

Reply

Leave a Comment

Tagged as: , , , , , , , ,

Previous Faq:

Next Faq: