Unix Technical Forum

How to Execute SQL Scripts using Batch file?

This is a discussion on How to Execute SQL Scripts using Batch file? within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, How can we execute SQL Scripts using Batch file??? i think Batch file should contain Username,Password,Database and Scripts... ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:42 PM
raghutumma@gmail.com
 
Posts: n/a
Default How to Execute SQL Scripts using Batch file?

Hi,

How can we execute SQL Scripts using Batch file???

i think Batch file should contain Username,Password,Database and
Scripts...
Using that file scripts should run...

How can i give UserName,Password,Database and all those things?

Plz send me details how to do that...if possible with example...


Thanx in advance,
RR...

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:42 PM
Erland Sommarskog
 
Posts: n/a
Default Re: How to Execute SQL Scripts using Batch file?

(raghutumma@gmail.com) writes:
> How can we execute SQL Scripts using Batch file???
>
> i think Batch file should contain Username,Password,Database and
> Scripts...
> Using that file scripts should run...
>
> How can i give UserName,Password,Database and all those things?
>
> Plz send me details how to do that...if possible with example...


From a .BAT file you can use the command-line tools OSQL or SQLCMD. They
are largely identical, but SQLCMD has some extra bells and whistles. On
the other hand SQLCMD only comes with SQL 2005.

For an overview over command-line options, run OSQL -? or SQLCMD -? at
the command line. For more details, look them up in Books Online.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 03:42 PM
Dan Guzman
 
Posts: n/a
Default Re: How to Execute SQL Scripts using Batch file?

> How can i give UserName,Password,Database and all those things?
>
> Plz send me details how to do that...if possible with example...


To expand on Erland's response, you can pass parameters on the command line
and reference using %1 through %9. The examples below execute all the
".sql" scripts in the current folder using the server, database, user and
password passed as command-line arguments.

FOR %%f IN(*.sql) DO SQLCMD -S %1 -d %2 -U %3 -P %4 -I -i "%%f"

FOR %%f IN(*.sql) DO OSQL -S %1 -d %2 -U %3 -P %4 -i "%%f"

--
Hope this helps.

Dan Guzman
SQL Server MVP

<raghutumma@gmail.com> wrote in message
news:1192032042.088785.7800@v3g2000hsg.googlegroup s.com...
> Hi,
>
> How can we execute SQL Scripts using Batch file???
>
> i think Batch file should contain Username,Password,Database and
> Scripts...
> Using that file scripts should run...
>
> How can i give UserName,Password,Database and all those things?
>
> Plz send me details how to do that...if possible with example...
>
>
> Thanx in advance,
> RR...
>
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:02 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com