Views

  • Comments posted to this topic are about the item Views

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • [font="Verdana"]Dear Author,

    if you consider the following ... it would be syntax error

    CREATE VIEW A_TEMP AS

    WITH RESULTSET AS (SELECT * FROM SYS.ALL_OBJECTS WHERE TYPE = 'U')

    and if you consider the following

    CREATE VIEW A_TEMP AS

    WITH RESULTSET AS (SELECT * FROM SYS.ALL_OBJECTS WHERE TYPE = 'U')

    SELECT * FROM RESULTSET

    it means good you have done.

    I conceived your quest statement as 'The Single Defining Select Statement of View Definition' as shown in code segment 1 above.

    And i consider the 2nd code segment "SELECT * FROM RESULTSET" as 2nd select statment with in the view defintion.

    So please let try to elaborate your quest further so ppl can get really enjoy these quests.

    Thank you for next time elaboration.

    [/font]

  • Good, clean question, Ron. Nice work!

    abrar.ahmad-1058946 (4/21/2010)


    I conceived your quest statement as 'The Single Defining Select Statement of View Definition' as shown in code segment 1 above.

    The question explicitly states: "Can a View be created using a CTE as part of its defining SELECT statement.?" The emphasized part makes it very clear that the question was not about creating a view using only a CTE as its definining SELECT statement.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • [font="Verdana"]

    Hugo Kornelis (4/21/2010)


    Good, clean question, Ron. Nice work!

    abrar.ahmad-1058946 (4/21/2010)


    I conceived your quest statement as 'The Single Defining Select Statement of View Definition' as shown in code segment 1 above.

    The question explicitly states: "Can a View be created using a CTE as part of its defining SELECT statement.?" The emphasized part makes it very clear that the question was not about creating a view using only a CTE as its definining SELECT statement.

    following seems cool to me

    Do body of a view can contain CTE/s?

    Do we can use CTEs within a view defintion?

    But i appreciate your "emphasized part" understanding, and would try to correct myself in future.

    Thanks buddy.

    [/font]

  • Good question today. No scope for criticism 🙂

  • Many times the questions are ambiguous or semantically misleading. This was not one of those times. Also, I learned something new. Thanks for the good question.

  • Good question. I agree, no scope for criticism. Also, I have made such a view, so I know it can be done. 🙂

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • Nice QOD! Does anyone have a link to a view that uses a CTE in its definition? I had never even considered doing this and would like to see an example if anyone has a fairly simple one.

    Thanks,

    Kenny

  • Kenneth Wymore (4/21/2010)


    Does anyone have a link to a view that uses a CTE in its definition?

    Go to the page referenced in the explanation of the question; scroll all the way down to the community content; check out the second example.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Good QOD:-)

    TNX

    Michael

  • Oops, I glossed over that before. Thanks Hugo!

  • Good straight forward question! Thanks!

    ---------------------------------------------------------------------
    Use Full Links:
    KB Article from Microsoft on how to ask a question on a Forum

  • Hugo Kornelis (4/21/2010)


    Kenneth Wymore (4/21/2010)


    Does anyone have a link to a view that uses a CTE in its definition?

    Go to the page referenced in the explanation of the question; scroll all the way down to the community content; check out the second example.

    I usually just look at help from Management Studio. I didn't realize that the website had community content. Thanks for pointing that out.

  • Good question! Thank you Ron.

    About 2 years ago I read the BOL reference article on common table expressions and since that time I clearly remember that it stated in the remarks section that "When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon". Since that time I got into habit of always starting the cte expression with a semicolon, i.e.

    ;with records (some_columns) as

    (

    select ...

    )

    Because of that reference I answered today's question incorrectly figuring that surely the statement like this:

    create view view_name as

    ;with records (some_columns) as

    (

    select ...

    ) -- etc

    will never fly due to that semicolon. This taught me a lesson to read the BOL remarks more carefully. The one about semicolon talks about the batch but if the view definition has only one statement in it then no semicolon is required before the cte.

    Oleg

  • Thanks Ron. This was a good question.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

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

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