SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


computed column in view


computed column in view

Author
Message
ohpenot
ohpenot
SSC-Enthusiastic
SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)

Group: General Forum Members
Points: 134 Visits: 106
I have the following "create view" query that s not working, can't figure out what's wrong... :

use test1
go

create view dbo.GlobaldataEOLT2

as

select [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) -2
ELSE day([date_passage]) -1
END

FROM dbo.T_EOLT_BRUTE_FLOAT
laurie-789651
laurie-789651
UDP Broadcaster
UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)

Group: General Forum Members
Points: 1448 Visits: 1272
When you say it's not working - do you mean it errors or it produces unexpected results?
laurie-789651
laurie-789651
UDP Broadcaster
UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)

Group: General Forum Members
Points: 1448 Visits: 1272
How about = instead of 'AS'




create view dbo.GlobaldataEOLT2

as

select [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) -2
ELSE day([date_passage]) -1
END

FROM dbo.T_EOLT_BRUTE_FLOAT




or



CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(jour_passage) -2
ELSE day([date_passage]) -1
END as [Jourprod]



asiaindian
asiaindian
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1602 Visits: 1156
Could you please recheck the CASE statement...I think it should be..

-- [Jourprod] AS
CASE WHEN heure_passage BETWEEN '00:00:00' and '05:59:59' THEN day(jour_passage) -2
ELSE day([date_passage]) -1
END AS [Jourprod]
ohpenot
ohpenot
SSC-Enthusiastic
SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)

Group: General Forum Members
Points: 134 Visits: 106
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 jourprod

FROM dbo.T_EOLT_BRUTE_FLOAT

Thanks Laurie, Asiaindian!
ohpenot
ohpenot
SSC-Enthusiastic
SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)

Group: General Forum Members
Points: 134 Visits: 106
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.GlobaldataEOLT8
GO

I 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.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62499 Visits: 17959
ohpenot (2/25/2013)
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.GlobaldataEOLT8
GO

I 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.


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.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sergiy
Sergiy
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25572 Visits: 12467
Sean Lange (2/25/2013)
.. you would need to add the computed column to your base table.

or create an indexed view.

There are requirements to meet though - check BOL for details.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62499 Visits: 17959
Sergiy (2/25/2013)
Sean Lange (2/25/2013)
.. you would need to add the computed column to your base table.

or create an indexed view.

There are requirements to meet though - check BOL for details.


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.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Sergiy
Sergiy
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25572 Visits: 12467
Sean Lange (2/27/2013)
. A view does not store any data so the calculation would not be stored.

An indexed view does.
Another name for it is "materialized view" - because all the calculations and conversions are actually stored in the index(es).
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search