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.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • Faris Zuriekat, M.S Jan 30, 2009 @ 23:58

    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’

  • Steve Atkinson Mar 22, 2012 @ 14:51

    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 Jun 2, 2015 @ 18:24

      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 Jun 20, 2012 @ 10:28

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

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

  • Harshita Aug 14, 2012 @ 8:42

    Thankss for the info..

  • Sudarshan Aug 24, 2012 @ 6:02

    @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 Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.