Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2002

    Question Unanswered: Result From SQL Statement For .bat File

    Hello All.

    I would like to do a count (within a (.bat) batch file) on a particular table in my SQL database 7.0 and if the result is zero, I will not process the remaining lines in the batch file.

    Anyone able to advise on this?

    Thank you.

    Best regard
    Teck Boon

  2. #2
    Join Date
    Apr 2002

    Re: Result From SQL Statement For .bat File

    I don't think you are going to be able to access the DB directly inside a batch file. I would suggest writing an app which access' the DB, and counts the rows. Whith this you could either get the batch file to act on the value reurned or get the app to write a new batch file to be run (either containing or ommiting the required lines).
    Last edited by pickledAvenger; 10-14-02 at 09:34.

  3. #3
    Join Date
    Feb 2002
    Houston, TX
    I do this all the time... You will need to give some thought on how your test will work though. Read up on I/OSQL in Books Online. Here is some code to play with:

    the SP...
    create procedure TestCMD(
    @Parameter  int = 0)
    declare @Error int
    if (@Parameter = 0) begin
      print 'Okay'
      set @Error = 0
    end else begin
      raiserror('@Parameter:%d.',11,1,@Parameter) with nowait
      set @Error = 1
    return @Error
    the .CMD...
    Please note the %1, this allows you to run the .CMD file and pass diffrent parameter values
    osql -S <your server> -E -d <your db> -Q"TestCMD %1" -b
    echo %ERRORLEVEL%
    osql -S <your server> -E -d <your db> -Q"EXIT(if (select count(*) from sysobjects) <> 0 raiserror('Not Okay!',11,1) with nowait)" -b
    echo %ERRORLEVEL%

    Did you know that .BAT files run in a 16bit environment while .CMD files run in a 32bit environment?
    Last edited by Paul Young; 10-14-02 at 11:02.
    Paul Young
    (Knowledge is power! Get some!)

  4. #4
    Join Date
    Jun 2002

    Cool Result From SQL Statement For .bat File

    Hello Paul.

    Thank you for your codes and advise. I have tried them and they worked perfect in my batch file. I am now able to control what needs to be done or not done based on result received from SQL.

    You are GREAT !!!! Thanks a million.

    Best regards
    Teck Boon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts