MS-SQL: Run a SQL Script on Database Server

last updated in Categories , ,

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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

6 comment

  1. 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’

  2. 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…

    1. 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.

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

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

  4. @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

    Still, have a question? Get help on our forum!