Monday, January 7, 2008

How to run a script file from MS SQL Query Analyzer

No direct command can be use to read a script file and execute it. But the isql.exe and osql.exe come in handy when you have to execute a script file from within T-SQL. Just call any of these exes using xp_cmdshell and pass the script file name as parameter to it. See SQL Server Books Online for more information about the input parameters of these exes. Here are some quick examples:

EXEC master..xp_cmdshell 'osql /d DatabaseName /U UserName /P Password /i C:\YourFileNameWithExt -n'

See xp_cmdshell in SQL Server Books Online if you are having permissions problems in getting this technique to work

Hope this will work.


Zaq

4 comments:

Mamun said...

Hi Zaq,
I tried to run the command that u have sent bt i failed can u write detail about the command.

Mamun

Mamun said...

Hi ZAQ,
I tried to run the command but i failed can u write more detail about the command
Mamun

Zakirul Quayum said...

Hi Mamun,

Thanks for your comments. You can run this line from your OS commnad prompt. Then just write this -

osql /d databasename /U username /P password /I C:\ScriptfileLocationWithExtension

Here you can notice that i ignore the last param "-n". You just dont need to use it. But if you want to run this from Query Analyzer then you can use the total line. I think it'll work. If you find any further problem feel free to write me.

Thanks once again for writting me.

zaq

Anonymous said...

Hey,

Thanks buddy. I was looking that type of shortest solution.

Loon.