Baskar B.V

SQL - BI Consultant

  Home  |   Contact  |   Syndication    |   Login
  23 Posts | 0 Stories | 21 Comments | 0 Trackbacks

News

Baskar

Archives

Blogs

SQL-BACKUPS

SQL-PERFORMANCE

SQL-SERVER

In this post i am sharing my experience in executing SQL scripts using the utility SQLCMD of SQL-SERVER 2005. In Sql-Server 2000 osql was used to achieve this.

In projects we come across situation wherein we need to decide about the solution of how to run our database scripts. One way is to Generate Scripts in Sql-Server management studio and save these as .sql files and running from the database server. This post is going to explain a similar approach for this.

Consider for e.g we had created separate scripts for dropping tables,constraints etc., table creation scripts, stored procedure scripts all compiled in separate .sql files. Instead of running these .sql files separately in Management Studio we can create a separate master sql script .sql wherein we could call all the child scripts using sqlcmd command.

The below snapshot shows how to achieve this.

 

 

 

 

The command used to call .sql file is :r.  Before you execute you need to enable SQLCMD mode either from toolbar or from Query Menu. After :r command pass the .SQL path with file name within quotes.

One weird this i found is when we are executing stored procedures written as .SQL files if it does not find an GO statement at the end of the first .SQL file it would take the script from the other .SQL file till GO statement and appends that script at the end of the first stored procedure.

Hope this post helps.  Provide your comments.

Happy Coding!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Tuesday, June 26, 2007 12:50 PM

Feedback

# re: Executing SQL file using SQLCMD command 6/26/2007 5:20 PM David
Why not just use a SQL stored procedure? It will mean that you don't have to have any sort of files, and unlimited number of users can modify the *latest* version of the SP at any time.

This sounds like the hard way of doing things. Can you explain why you are using files over SP?

# re: Executing SQL file using SQLCMD command 6/27/2007 2:12 AM Baskar
Hi,

The solution i talked about was running scripts when moving database to production or so. The reason i prefer SQL over SP is easy to run scripts when moving it production. Suppose you have 100 SP in your project and how do u deploy it in the server? You cannot open all the SP's over there and run each one by one. This SQL file is nothing but SP only saved as .SQL file. Even for modification people can take the latest SQL file and modify the SP inside it and run the SQL file.

# re: Executing SQL file using SQLCMD command 1/12/2008 1:12 AM Scott Bateman
Any tricks on how to run .sql files on a network? By default, it appears that SQLCMD doesn't like UNC paths.

# re: Executing SQL file using SQLCMD command 1/13/2008 12:54 PM Baskar
Did you try using -i switch like

-i \\<networkserver>\<foldername>\<filename>

P.S. If the file path has spaces then we need to include them within quotes. Let me know if that works out.

# re: Executing SQL file using SQLCMD command 5/27/2009 12:54 AM mohan
i want schedule the script. here i have to enable the sqlcmd at runtime using t-sqlscript.
Is there any script for enabling SQL CMD Mode??????

# re: Executing SQL file using SQLCMD command 10/12/2010 12:40 AM Dheeraj Gupta
Hi,

Can we use relative file path Instead of absolute file path.
Suppose I have created a master.sql file in folder ABC.
This ABC folder also contains File1.Sql, File2.sql and File3.sql.
Now, Instead of providing full path, can we use relative path so that my Master.sql file for the files in its own folder i.e. ABC folder

# re: Executing SQL file using SQLCMD command 12/28/2010 3:33 AM Shehanaz
it is very usefull
thanks
i need script to enable SQLCMD Mode

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: