pass parameters to osql RRS feed

  • Question

  • Hi guys,

    I need to execute osql statement from .bat file. I need to delete rows from SQL Server 2008 table between exact dates. These datess are passed to .bat file as parameters. Inside bat file I have such statement:

    osql -S%1% -E -d DBMI_Consolidated -Q"delete from volumes_market_STP_debt WHERE MI_Date between %3 and %4" -I -b

    But it doesnt work. The same command but without passing parameters works. Where is the mistake?


    Wednesday, April 25, 2012 2:41 PM


All replies

  • what error do you get ?

    and why are you using -S%1% and not just -S %1

    Wednesday, April 25, 2012 7:18 PM
  • Hi,

    I would strongly recommend that you consider switching over to using the SQLCMD utility - http://msdn.microsoft.com/en-us/library/ms162773(v=sql.100).aspxosql was introduced in SQL Server 2000 version. sqlcmd is newly added in SQL Server 2005 and offers additionally functionality which SQL Server 2005 offers. here are many more advantage of using sqlcmd - http://blog.sqlauthority.com/2009/01/05/sql-server-sqlcmd-vs-osql-basic-comparison/ 

    However, if you still wish to continue using osql, i believe the problem in your statement is how you have set up the parameters %3 & %4.

    If you issue a query from a batch file, use %variables, or environment %variables%. For example:

    SET table = sysobjects
    osql /q "Select * from %table%"


    Hope this helps !

    Sanil Mhatre | Database Developer | MCTS | If you find my reply useful in any way, please vote it as helpful. If it has helped answer your question, please mark it as Answer. www.sqlwithsanil.com

    • Proposed as answer by Iric Wen Thursday, April 26, 2012 6:14 AM
    • Marked as answer by SunnyInga Thursday, April 26, 2012 12:03 PM
    Wednesday, April 25, 2012 9:00 PM