Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a script using a BAT file?

I would like to have a BAT file open a sql server script. Currently I have this code in the sql file:

declare @path varchar(255), @mydb varchar(50)
SELECT @mydb = 'timeclockplus'
select @path = 'C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Backup\' 
            + @mydb + '-' + convert(varchar(8),getdate(),112) + '.bak'
BACKUP DATABASE @mydb TO DISK = @path

How do I open this SQL file from a BAT file?

I am currently trying to run it like this:

C:\Program Files\Microsoft SQL Server\80\Tools\Binn\osql -E 
   -S Sql server-hl7\timeclockplus timeclockplus.sql -oresults.txt

but OSQL does not exist in the BINN directory,

like image 664
Alex Gordon Avatar asked Jul 15 '10 19:07

Alex Gordon


1 Answers

You should invoke the sqlcmd command-line tool from your batch file. Assuming your sql file is "backup.sql", the command line would be something like:

sqlcmd -E -S yoursqlinstance -i backup.sql

-E uses trusted connection, replace with -U and -P if you need to specify a SQL username and password. See also this article with examples.

like image 57
driis Avatar answered Oct 09 '22 13:10

driis