Can be CTE referenced within view?

  • CTE is not stored in the SQL Server metadata. I need the CTE as a source for Crystal Reports.

    Can somebody give some ideas?

    Thank you!!

  • The quickest way to answer that question is to try it. 😛

    Yes, a CTE can be used in a view. You should then be able to reference the view in CR.

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • Thank you Jason for the reply.

    I can't find examples CTEs within views. Tried to create - CTE has to be started with WITH and semicolon before! I am really confused, trying...

    Has somebody some examples of views with CTE within??

    Thanks very much!

  • CREATE VIEW dbo.cte_view_test

    AS

    WITH cte_test

    AS

    (SELECT id, val FROM dbo.test)

    SELECT

    id,val

    FROM cte_test

    Where test is your table

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • Here you go. This is using AdventureWorks. Have fun:

    CREATE VIEW y

    AS

    WITH x AS (SELECT *

    FROM [HumanResources].[Employee]

    )

    SELECT * FROM x

    GO

    SELECT * FROM y

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Thank you Grant Fritchey

    Busy with another urgent issue. I will have fun later.

    Thanks!!

  • Hmmm I think I saw this request before 😉


    * Noel

Viewing 7 posts - 1 through 6 (of 6 total)

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