Results 1 to 6 of 6

Thread: SQL Views with Computed Columns

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    SQL Views with Computed Columns

    Hi Guys,

    Is it possible for me to create views with persisted column?

    Code:
    CREATE VIEW dbo.viewTable AS
    SELECT
       Item1,
       Item2,
       Total AS (Item1+Item2) PERSISTED
    FROM
       Table1 t1
    JOIN
       Table2 t2
    ON
       t1.ID = t2.ID
    Also what is the proper syntax for this?
    Last edited by jsc0624; Jan 25th, 2009 at 04:45 AM.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Views with Computed Columns

    What version of MS SQL SERVER are you using?

    Or is this even MS SQL?

    VIEW's can have "computed" columns - that's kind of one of the major features of a VIEW.

    Give us more info on what your actual goal is here.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: SQL Views with Computed Columns

    Persisted is a new feature in SQL Server 2005 that if used will store the result of the computed column. Primarily used to allow indexing on computed columns.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Views with Computed Columns

    So PERSISTED takes the place of manually putting an INDEX on a computed column - which would have persisted it in the past.

    Is that kind of the point?

    I've used computed columns in a table once. Then eventually dropped the concept as it only made inserting rows into that table painful.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: SQL Views with Computed Columns

    Sorry, misread the docs. Persisted is required if the computed column is based on a CLR expression and you want it to be indexed.

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: SQL Views with Computed Columns

    Why are you designing indexes on computed columns rather than on keys? There are other tuning methods such as checking query explain plan, disk I/O, etc, creating an index is not a one size fits all solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width