Return

  • Nice, very nice. Thanks, Carlo!

  • Interesting question thanks:-)

    I think it also provides a good argument for the use of statement terminators or formatting scripts in a clear manor (with comments:-D).

    Batch one appears to have 3 statements rather than 4, due to the way it is formatted. If it had been written as select 1;

    return; select 2;

    select 3; or

    --First Query

    select 1

    -- Exit Batch

    return

    -- Other queries

    select 2

    select 3 it would be very clear that 'select 2' is not part of the return statement.

  • Primo Dang (7/17/2013)


    L' Eomot Inversé (7/17/2013)


    The reason that you can't use return with a value in a batch is that batches don't return scalar values, they return (possibly empty) result sets or they don't return anything or they raise an error.

    Thanks for the explanation, Tom. I had no idea batches could return anything at all.

    Oops, I got that wrong; the first of the three things they can return is a set of possibly empty result sets, not necessarily just one possibly empty result set. They have to be able to do that, or embedded SQL (including proc calls from outside SQL) would not be very useful.

    Also, thank you Carlo for the question. I loved the trick with non-existing table! 😀

    Yes, I liked that too. Definitely fun, and also definitely something that will help people understand what RETURN does.

    edit: add a closing ] to a quote tag

    Tom

  • mickyT (7/17/2013)


    Interesting question thanks:-)

    I think it also provides a good argument for the use of statement terminators or formatting scripts in a clear manor (with comments:-D).

    Batch one appears to have 3 statements rather than 4, due to the way it is formatted. If it had been written as select 1;

    return; select 2;

    select 3; or

    --First Query

    select 1

    -- Exit Batch

    return

    -- Other queries

    select 2

    select 3 it would be very clear that 'select 2' is not part of the return statement.

    Yes, that's right (apart from the crazy idea that statement terminators have any place in a properly designed language - although maybe that's irrelevant, because SQL certainly isn't such a language and statement terminators maybe do have a place there, since the terribleness of T-SQL has now increased to the point where it actually needs them. But in my experience it's necessary to be able to interpret SQL that is badly laid out and doesn't have comments that indicate what is going on. It's sad, but when you find yourself looking after a database which someone who has not been educated properly about relational principles and general development principles your are going to have a lot of code like that that you need to get to understand, so questions using such horribly written SQL are actually a good thing, because they help us recognise that this is something we will have to cope with and give us practise in coping with it.

    Tom

  • mickyT (7/17/2013)


    Interesting question thanks:-)

    I think it also provides a good argument for the use of statement terminators or formatting scripts in a clear manor (with comments:-D).

    Batch one appears to have 3 statements rather than 4, due to the way it is formatted. If it had been written as select 1;

    return; select 2;

    select 3; or

    --First Query

    select 1

    -- Exit Batch

    return

    -- Other queries

    select 2

    select 3 it would be very clear that 'select 2' is not part of the return statement.

    Another use of RETURN in a batch is with IF:

    SELECT 1

    -- only on test db

    IF DB_NAME() NOT LIKE '%_TEST'

    BEGIN

    print 'WARNING: run only on test db'

    RETURN

    END

    ALTER TABLE MY_TBL ADD ID_TEST INT NULL

    -- follows other stmts

  • Note to self - read question clearly before answering, including how many choices to make.....doh!!! Not a good start to the day - I knew the answer but only selected one option.....:w00t:

  • nice question.. new things added to my database...

    Manik
    You cannot get to the top by sitting on your bottom.

  • Nice question

    Thanks

  • Good one.. thanks for posting.

    -RP
  • Nice question. Good information about return functionality. 🙂

  • very nice thanks

    especially "RETURN SELECT * FROM non_existing_table" !!

Viewing 11 posts - 16 through 25 (of 25 total)

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