Execute the generated sql file
Hello,
I have written a VB macro in Microsoft Word. The function of the macro is to read a word document(which has tables) and generate the corresponding sql files with many sql statements in the same folder with .sql extension. Now, the expected functionallity is "when I run a macro it should generate sql files and also execute the generated sql file".
Can this be done?
I am new to VB. So please help proceed with this.
Thanks
Re: Execute the generated sql file
If you double-click on a .sql file in Explorer does it automatically execute? (or do you get an option to execute when you right-click on it?)
If so, you can reproduce that behaviour by using ShellExecute, as shown in the article How do I open a file/web-page in its default application? from our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page)
Re: Execute the generated sql file
That'll open the SQL file.... but I don't think it'll run it... BUT.... you can shell out to osql.exe (which is in the bin/tools fodler where ever SQL is install) and pass it the necessary parameters (server, user, password & sql file) and it'll run it for you.... I've never actually done it this way, so I can't give you specifics, but you should be able to find all the info on it in MSDN.
-tg
Re: Execute the generated sql file
i've done executing .sql file in VB.NET but not in VBA but i guess it's the same...
it's using osql command
simply make a batch file with .cmd extension and try put this code inside
Code:
osql -S %1 -d %2 -E -b -i "PATH_TO_SQL_FILE\SQL_FILE.sql"
see this to change %1 and %2 to correct value
then execute those bat file using shell32 or double click it
PS: in .NET 2003 it's easier because all we have to do is add the .sql and create command file :D
HTH
Re: Execute the generated sql file
Thank you very much.
I created a batch file and in the batch file I called sqlplus along with the userid/password. In my VB macro I used the shell command and passed the path of the batch file and the path of the sql which has got generated as parameters and it is executing the generated sql file also.
Batch file :
sqlplus userid/password@schema %1
shell command:
shell temp.bat & " " & sql_file
Thanks for guiding me.