SQL Server Performance

Using variables in View object

Discussion in 'SQL Server 2005 T-SQL Performance Tuning' started by sqldev, Jun 26, 2007.

  1. sqldev New Member


    Is it possible to do any programming in the View object, for example, in the code snippet given below, I would like to find out the date manually and execute an SQL statement based on that. I cannot call any procedure before running this view to store the data in a temp. table and refer it from view.

    Create View dbo.vw_GetAvgPrevYear

    declare @dt_LastYearDate as datetime
    set @dt_LastYearDate = '2007-06-25 20:39:00'

    select * from Employee
    where ProcessDate = @dt_LastYearDate


    Please give me your suggestions, thank you.

    Devendran R
  2. Madhivanan Moderator

    You cant use variables in a view. Use procedure or function instead


    Failing to plan is Planning to fail
  3. Adriaan New Member

    Of course you can create a view that works with relative criteria, like 'today' or 'last month'.

Share This Page