Execute sql statements using batch file

  • Hello guys,

    I have a server which has sql express installed. It only has database engine and does not have sql agent. I need to backup all the databases for this instance everyweek and cannot automate it as there is no agent installed. Is there a way of executing .sql file from batch file. I can then schedule the batch file from windows scheduling agent. If possible can you also tell me the syntax as I tried to google it but could not get the syntax anywhere.

    thanks a tonn,

    Nikhil

  • Does sql express have the sqlcmd.exe installed? If so, you can use that.

  • Thanks for the reply,

    SQLCMD is not installed

  • Thanks for the reply,

    I dont think I can install anything on this server, so that is the reason I used to take manual backups instead. I read stuff which says it is possible but could not find out how? Do you know how to execute SQL script using batch file.

    Thanks a ton,

    Nikhil

  • You just need to install the sqlcmd.exe whereever you want to run the backups from.

    If you open cmd and type sqlcmd /? (of course you need to be in the bin directory where sqlcmd is installed or have that in your path environment variable somewhere)

    You would create the backup script and save that to backupscript.sql file

    Create your .bat file something like this:

    sqlcmd -s Myserver -UMyUser -PMyPassword -i backupscript.sql -o outputfile.txt

    You could also use integrated authentication by using the -E switch, but remember, it will run under the context of whatever account you use to schedule it. The account will also need backup permissions - db_backupoperator in the desired databases.

    Hope this helps!

  • you can use osql utility for running your script as a batch..

    Shekhar..:)


    Thanks ,

    Shekhar

  • Thanks a tonn dude,,

    for all the help.. it worked....

  • osql uses odbc

    sqlcmd is the new replacement which uses ole db. I would assume it would be the wisest choice to use - it would probably give your solution the longest life.

    "The osql utility uses the ODBC database application programming interface (API). It is a replacement for the isql command prompt utility based on the DB-Library API. Both utilities are provided with Microsoft SQL Serverâ„¢ 2000. The DB-Library API remains at a SQL Server 6.5 level; therefore, applications that depend on DB-Library, such as isql, do not support some SQL Server 2000 features. For example, isql cannot access columns defined with the ntext data type and truncates any char, varchar, nchar, or nvarchar columns longer than 255 bytes. It also cannot retrieve results as XML documents. Except for these limitations in isql, both osql and isql support the same features. For more information about features not supported by isql, see Connecting Early Version Clients to SQL Server 2000."

  • Thanks for tht information.

    Nikhil

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply