≡ Menu

MS-SQL: Run a SQL Script on Database Server

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.

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 6 comments… add one }

  • Faris Zuriekat, M.S January 30, 2009, 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’

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

  • Steve Atkinson March 22, 2012, 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…

    • Todd June 2, 2015, 6:24 pm

      given any dba task, I’ll bet I can do it in sql server twice as fast as you can do it in oracle. And I won’t have to involve two other dbas in the process either.

  • Dinesh vishe June 20, 2012, 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’ ????

  • Harshita August 14, 2012, 8:42 am

    Thankss for the info..

  • Sudarshan August 24, 2012, 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

Leave a Comment