﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2012 / SQL Server 2012 -  T-SQL  / computed column in view / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 11:28:31 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: computed column in view</title><link>http://www.sqlservercentral.com/Forums/Topic1422624-3077-1.aspx</link><description>Thank you very much for all your answers. That's what i need.I am going to work with indexed views. Except that i finally decided to add certain computed columns directly to base tables, so that i can focus on aggregate functions in views.</description><pubDate>Fri, 01 Mar 2013 04:46:06 GMT</pubDate><dc:creator>ohpenot</dc:creator></item><item><title>RE: computed column in view</title><link>http://www.sqlservercentral.com/Forums/Topic1422624-3077-1.aspx</link><description>[quote][b]Sean Lange (2/27/2013)[/b][hr]. A view does not store any data so the calculation would not be stored. [/quote]An indexed view does.Another name for it is "materialized view" - because all the calculations and conversions are actually stored in the index(es).</description><pubDate>Wed, 27 Feb 2013 21:31:07 GMT</pubDate><dc:creator>Sergiy</dc:creator></item><item><title>RE: computed column in view</title><link>http://www.sqlservercentral.com/Forums/Topic1422624-3077-1.aspx</link><description>[quote][b]Sergiy (2/25/2013)[/b][hr][quote][b]Sean Lange (2/25/2013)[/b][hr].. you would need to add the computed column to your base table.[/quote]or create an indexed view.There are requirements to meet though - check BOL for details.[/quote]Not sure I follow you here. You don't need to have an indexed view to have a calculation in it. The point of using PERSISTED is so that the engine does not have to calculate the value every time you retrieve a row. It does this by physically storing the results in the table. A view does not store any data so the calculation would not be stored. I agree though that an indexed view sounds like the best solution for the OP's situation.</description><pubDate>Wed, 27 Feb 2013 07:53:12 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: computed column in view</title><link>http://www.sqlservercentral.com/Forums/Topic1422624-3077-1.aspx</link><description>[quote][b]Sean Lange (2/25/2013)[/b][hr].. you would need to add the computed column to your base table.[/quote]or create an indexed view.There are requirements to meet though - check BOL for details.</description><pubDate>Mon, 25 Feb 2013 20:29:21 GMT</pubDate><dc:creator>Sergiy</dc:creator></item><item><title>RE: computed column in view</title><link>http://www.sqlservercentral.com/Forums/Topic1422624-3077-1.aspx</link><description>[quote][b]ohpenot (2/25/2013)[/b][hr]Another problem linked to this one..The query works fine now, even if i don't like not being able to write "jprod as....query treatement here"What if i need to define the computed column "jprod" as persisted?I tried to put the keyword PERSISTED wherever it could be and i got an error in each case.SELECT [NUMCAM] 	  ,[date_passage]       ,[heure_passage] 	  ,[anneeprod]	  ,(CASE  WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(date_passage) - dbo.jprodtestannee(anneeprod)		ELSE day(date_passage) - dbo.jprodtestannee(anneeprod) +1	   END) as  jprod FROM dbo.GlobaldataEOLT8GOI tried to re-write the query the way i feel more comfortable with :..jprod as (CASE WHEN....) PERSISTED but the error remains, can't get it solved.[/quote]PERSISTED is used for computed columns. You are creating a view, this means the calculation will have to run everytime you select the data. If you want to avoid this you would need to add the computed column to your base table.</description><pubDate>Mon, 25 Feb 2013 09:15:02 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: computed column in view</title><link>http://www.sqlservercentral.com/Forums/Topic1422624-3077-1.aspx</link><description>Another problem linked to this one..The query works fine now, even if i don't like not being able to write "jprod as....query treatement here"What if i need to define the computed column "jprod" as persisted?I tried to put the keyword PERSISTED wherever it could be and i got an error in each case.SELECT [NUMCAM] 	  ,[date_passage]       ,[heure_passage] 	  ,[anneeprod]	  ,(CASE  WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(date_passage) - dbo.jprodtestannee(anneeprod)		ELSE day(date_passage) - dbo.jprodtestannee(anneeprod) +1	   END) as  jprod FROM dbo.GlobaldataEOLT8GOI tried to re-write the query the way i feel more comfortable with :..jprod as (CASE WHEN....) PERSISTED but the error remains, can't get it solved.</description><pubDate>Mon, 25 Feb 2013 08:57:34 GMT</pubDate><dc:creator>ohpenot</dc:creator></item><item><title>RE: computed column in view</title><link>http://www.sqlservercentral.com/Forums/Topic1422624-3077-1.aspx</link><description>Indeed!It works this way:               select               ,....              ,[version_prog]	  ,(CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN (day(date_passage) -2)ELSE (day([date_passage]) -1)END) AS jourprodFROM dbo.T_EOLT_BRUTE_FLOATThanks Laurie, Asiaindian!</description><pubDate>Thu, 21 Feb 2013 08:40:33 GMT</pubDate><dc:creator>ohpenot</dc:creator></item><item><title>RE: computed column in view</title><link>http://www.sqlservercentral.com/Forums/Topic1422624-3077-1.aspx</link><description>Could you please recheck the CASE statement...I think it should be..-- [Jourprod] ASCASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(jour_passage) -2ELSE day([date_passage]) -1END AS [Jourprod]</description><pubDate>Thu, 21 Feb 2013 08:36:45 GMT</pubDate><dc:creator>asiaindian</dc:creator></item><item><title>RE: computed column in view</title><link>http://www.sqlservercentral.com/Forums/Topic1422624-3077-1.aspx</link><description>How about = instead of 'AS'[code="sql"]create view dbo.GlobaldataEOLT2asselect [NUMCAM],[C_Fourn],[date_passage],[heure_passage],[banc],[NUMPale],[Module_Status],[Code_defaut],[Total_TestTime],[Purgerunin_TestTime],[ShifterPosHyst_TestTime],[ShifterActRespTime_TestTime],[GearActStroke_TestTime],[GearActRespTime_TestTime],[LeakageON_TestTime],[K1K2Leakage_TestTime],[QPVK1DeliveryTest_TestTime],[PPVK2PressureTest_TestTime],[LeakageOFF_TestTime],[PRVTest_TestTime],[OPS_NRV_Test_TestTime],[version_prog],[Jourprod] =CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(jour_passage) -2ELSE day([date_passage]) -1ENDFROM dbo.T_EOLT_BRUTE_FLOAT [/code]or[code="sql"]CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(jour_passage) -2ELSE day([date_passage]) -1END as [Jourprod][/code]</description><pubDate>Thu, 21 Feb 2013 08:35:39 GMT</pubDate><dc:creator>laurie-789651</dc:creator></item><item><title>RE: computed column in view</title><link>http://www.sqlservercentral.com/Forums/Topic1422624-3077-1.aspx</link><description>When you say it's not working - do you mean it errors or it produces unexpected results?</description><pubDate>Thu, 21 Feb 2013 08:33:40 GMT</pubDate><dc:creator>laurie-789651</dc:creator></item><item><title>computed column in view</title><link>http://www.sqlservercentral.com/Forums/Topic1422624-3077-1.aspx</link><description>I have the following "create view" query that s not working, can't figure out what's wrong... :use test1gocreate view dbo.GlobaldataEOLT2asselect	[NUMCAM]	  ,[C_Fourn]      ,[date_passage]      ,[heure_passage]      ,[banc]      ,[NUMPale]      ,[Module_Status]      ,[Code_defaut]	  ,[Total_TestTime]      ,[Purgerunin_TestTime]      ,[ShifterPosHyst_TestTime]      ,[ShifterActRespTime_TestTime]      ,[GearActStroke_TestTime]      ,[GearActRespTime_TestTime]      ,[LeakageON_TestTime]      ,[K1K2Leakage_TestTime]      ,[QPVK1DeliveryTest_TestTime]      ,[PPVK2PressureTest_TestTime]      ,[LeakageOFF_TestTime]      ,[PRVTest_TestTime]      ,[OPS_NRV_Test_TestTime]      ,[version_prog]      ,[Jourprod] AS CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(jour_passage) -2ELSE day([date_passage]) -1ENDFROM dbo.T_EOLT_BRUTE_FLOAT</description><pubDate>Thu, 21 Feb 2013 08:24:02 GMT</pubDate><dc:creator>ohpenot</dc:creator></item></channel></rss>