What will this query return?

  • Comments posted to this topic are about the item What will this query return?

    Thanks,
    Shiva N
    Database Consultant

  • Thanks for the question dear.

    Vimal LohaniSQL DBA | MCP (70-461,70-462)==============================The greatest barrier to success is the fear of failure ** Success is a journey not a destination**Think before you print, SAVE TREES, Protect Mother Nature

  • Nice one, thanks for sharing.

    Thanks

  • Nice one, thanks.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • When I work with CTE, I get this error when I dont give smicolon, so it was easy to answer πŸ™‚

    Thanks for the question

  • pmadhavapeddi22 (9/4/2014)


    When I work with CTE, I get this error when I dont give smicolon, so it was easy to answer πŸ™‚

    Thanks for the question

    +1

  • Thank you for the post, Shiva, good one.

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • Carlo Romagnano (9/4/2014)


    pmadhavapeddi22 (9/4/2014)


    When I work with CTE, I get this error when I dont give smicolon, so it was easy to answer πŸ™‚

    Thanks for the question

    +1

    +1

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • This was removed by the editor as SPAM

  • I think the explanation need some work.

    CTEs doesn't ALWAYS need a semi-colon in front.

    This for example works fine:

    DECLARE @tmpVariable INT

    WITH cte

    AS

    (

    SELECT 1/0 as 'Result'

    )

    SELECT *

    FROM cte

    I'd like to know when a CTE needs semi-colon and not... I always though its only after a select from a table, but apparently not πŸ™‚

  • DrKiller (9/4/2014)


    I think the explanation need some work.

    CTEs doesn't ALWAYS need a semi-colon in front.

    This for example works fine:

    DECLARE @tmpVariable INT

    WITH cte

    AS

    (

    SELECT 1/0 as 'Result'

    )

    SELECT *

    FROM cte

    I'd like to know when a CTE needs semi-colon and not... I always though its only after a select from a table, but apparently not πŸ™‚

    +1, Nice question thou. Thanks for sharing

  • twin.devil (9/4/2014)


    DrKiller (9/4/2014)


    I think the explanation need some work.

    CTEs doesn't ALWAYS need a semi-colon in front.

    This for example works fine:

    DECLARE @tmpVariable INT

    WITH cte

    AS

    (

    SELECT 1/0 as 'Result'

    )

    SELECT *

    FROM cte

    I'd like to know when a CTE needs semi-colon and not... I always though its only after a select from a table, but apparently not πŸ™‚

    +1, Nice question thou. Thanks for sharing

    In the local help it says "When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon."

    but the linked article states this

    I know that is not a requirement to terminate every SQL Statement, but it does make it easier to read. However in most cases, other than readability the termination of the previous statement really doesn’t matter. . . In most cases.

    I never really tested this much, but in general I just follow the statement highlighted in bold.

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • Easy mozo πŸ™‚

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • Hany Helmy (9/4/2014)


    Easy mozo πŸ™‚

  • Thanks for the question.

    I'm actually surprised so many people have got this wrong.

    I say that because when I started using CTEs I was not accostumed to using the semicolon so I would ALWAYS get this error. I'm sure it happened to a lot of other folks when learning this feature.

    ---------------
    Mel. 😎

Viewing 15 posts - 1 through 15 (of 34 total)

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