PRINT statement

  • I have some long-running admin scripts that run every day on some of my servers.

    I have several print statments in the job that output status and the results of some of the steps. The PRINT statement doesn't seem to output to the screen (or a log file if run from a job) immediatly. I assume it waits until some buffer somewhere fills up then dumps it.

    Question: Is there a way to make it immediatly print when I say print, or am I stuck waiting for SQL to decide when it's ready?

    The Redneck DBA

  • I never did find a way to get the print statement to happen immediatly, but this article tells how to accomplish basically the same thing wiht RAISERROR with the NOWAIT option that I had never thought of:

    http://www.mssqltips.com/tip.asp?tip=1660

    The Redneck DBA

  • The RAISERROR with the NOWAIT clause is certainly a way to do it... and then, you have nothing but something printed on the screen. No way to do any real analysis... no way to really search for problem points over time.

    The best thing to do would be to build a log table and write to the log table instead of writing to the screen. Of course, you should capture the procname with little tricks like OBJECT_NAME(@@PROCID), etc, and maybe even make it so the row identity in the log is remembered so you can log the start and end time of each section of the code instead of having to do a self-join on the log table to find duration. The one I have at work is actually "multi-tiered"... shows start of job and end of job on a job header row. All other rows use that same job header ID to provide a common run (job) reference.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Agreed.

    The cases where I would use that RAISERROR would be when I'm running admin scripts that I only run very infrequently.

    I've done similar (though less fancy) things with a log table when I've got scripts that run as part of a scheduled job. That can be very handy if you have lots of exceptions.

    Another thing I like to do is specify an output file when I'm setting up job steps. That really helps with troubleshooting if you have your scripts providing good output when failures or exceptions happen.

    The Redneck DBA

  • Cool... thanks for the feedback, Jason.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 1 through 4 (of 4 total)

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