xp_sendmail rearranges Print and Select

  • My SQL 7 stored procedure displays its statistical results in a SELECT statement. I also put in an introduction of who it is and when it was run, as well as a legend at the end of various acronyms used. So the code looks like:

    print 'heading information'

    print convert(varchar(20), getdate())

    select AcronymField, count(*)

    from table

    group by AcronymField

    print 'TLA - three letter acronym'

    print 'ABC - means this spelled out'

    The legend at the end displays just between two Print lines of the heading.

    Could someone tell me how to stop this behavior and maybe why it happens?

    TIA

    Andre

  • The SQL Server executes your code it sees each print statement as and the select as a separate query. Since there is no flow control, there's no guarantee in which order the queries are executed. The query optimizer has to calculate an execution plan for the select staement, which may take slightly longer than the print commands. So SQL decides to print the Legend first while waiting for the select plan.

    [font="Verdana"]Markus Bohse[/font]

  • Thanks. That makes sense. How do I go about managing the flow control then?

Viewing 3 posts - 1 through 2 (of 2 total)

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