|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 9:02 AM
Points: 1,196,
Visits: 1,320
|
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: 2 days ago @ 7:11 AM
Points: 877,
Visits: 1,159
|
|
Good question. If computed columns are persisted in nature (e.g. QTot AS QF1 + QF2 PERSISTED,) then you receive the error after "Insert Zeroes" print statement and "SELECT * FROM @Quantities" statement returns 2 rows - datasource Test1 & Test3.
Thanks
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Monday, August 13, 2012 10:04 AM
Points: 554,
Visits: 861
|
|
Thanks For the Good Question Tom Brown i learnt some thing new today and also i tried to create index on the computed columns it will not alllow to create index directly.
and i tried in this way
PRINT 'Define Table'; CREATE TABLE #Quantities ( ID INT IDENTITY, DataSource varchar(30) NOT NULL, QF1 Float, QF2 Float, QTot AS QF1 + QF2, PF1 AS QF1 / (QF1 + QF2), PF2 AS QF2 / (QF1 + QF2) );
CREATE NONCLUSTERED INDEX IXC1 on #Quantities(ID) INCLUDE(PF1,PF2)
PRINT 'Insert data'; INSERT INTO #Quantities (DataSource, QF1, QF2) SELECT 'Test 1' , 66, 34;
Print 'Insert Zeroes'; INSERT INTO #Quantities (DataSource, QF1, QF2) SELECT 'Test 2', 0, 0;
Print 'Insert Nulls'; INSERT INTO #Quantities (DataSource, QF1, QF2) SELECT 'Test 3', null, null;
PRINT 'Show Table'; SELECT * FROM #Quantities;
DROP TABLE #Quantities
i got the error while inserting the value it self
thanks again for the Good question
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 9:44 PM
Points: 1,852,
Visits: 987
|
|
Lost two points because of misunderstanding of set arthiabort on.I already worked divide by zero errors and handle these type of errors.but little bit confused.with arthabort you can handle this other wise using nullif also we can handle.Againg thanks for good question on basics.
Malleswarareddy I.T.Analyst MCITP(70-451)
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 3:15 AM
Points: 1,476,
Visits: 1,943
|
|
Great question.
However... why does SQL let something get stored (and im not talking about the computed column itself since that aint stored... unless its persisted) in the database in the first case, that it later cant read? Anyone know the reasoning behind that? (Besides performance that is)
/T
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 11:39 PM
Points: 1,133,
Visits: 856
|
|
In order to get round the divide by zero error, I generally do this:
PF1 AS QF1 / nullif((QF1 + QF2),0), instead of PF1 AS QF1 / (QF1 + QF2),
I.e. set the divisor to null, if the divisor is zero. I used to do complicated Case statements, but then I needed to retype the divisor. This way, I just add "NULLIF(" at the front and ",0)" at the end.
I'we written about it at Stack overflow
Best regards, Henrik Staun Poulsen www.stovi.com
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Yesterday @ 8:52 AM
Points: 1,788,
Visits: 3,327
|
|
| Stupid me, lost the points because I was thinking of persisted computed columns. Don't know why.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:03 PM
Points: 5,244,
Visits: 7,061
|
|
I totally agree with the other comments: good question. I wanted to add a comment about the effect of persisting the computed column (by adding the PERSISTED option or by including it in an index), but others have beaten me to it. 
tommyh (10/13/2010) why does SQL let something get stored (and im not talking about the computed column itself since that aint stored... unless its persisted) in the database in the first case, that it later cant read? Anyone know the reasoning behind that? (Besides performance that is) The developer chose not to persist the computed column. That means that the developer required SQL Server not to evaluate the expression until referenced. The only way for SQL Server to "know" that the expression can't be evaluated is by trying to evaulate it, so there is no way to tell when the data is stored if there will be an error - except if SQL Server chooses to disregard the developer's wish. Further, it might also be the case that the data stored is intermediate. The first stage of processing an order might involve copying the data from the order form; a later stage adds other data. A column that is only needed after the second stage could be a computed column that results in an error before the second stage is completed. Think about a computed column that lists the time between receipt of an order and shipping of the goods - while not producing an error, the computation would not result in a meaningful value if querie while the order is still being processed.
henrik staun poulsen (10/13/2010) In order to get round the divide by zero error, I generally do this:
PF1 AS QF1 / nullif((QF1 + QF2),0), instead of PF1 AS QF1 / (QF1 + QF2),
I.e. set the divisor to null, if the divisor is zero. I used to do complicated Case statements, but then I needed to retype the divisor. This way, I just add "NULLIF(" at the front and ",0)" at the end. That would be my suggestion as well. While the suggestion in the explanation works as well, it requires the person who queries the data to know that the column is computed, AND to know the exact expression. Writing the expression so that there can never be an error encapsulates this knowledge and reduces the risk of errors when new people start writing queries. BTW, the NULLIF function is simply a shorthand for the complicated CASE expressions you used to do. The NULLIF reduces the amount of code to maintain, but does not offer any performance benefits.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 12:56 AM
Points: 1,972,
Visits: 1,822
|
|
henrik staun poulsen (10/13/2010) In order to get round the divide by zero error, I generally do this:
PF1 AS QF1 / nullif((QF1 + QF2),0), instead of PF1 AS QF1 / (QF1 + QF2),
Good. The computed column should be NULL, if expression is invalid.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Yesterday @ 8:31 AM
Points: 3,129,
Visits: 4,312
|
|
good question. thanks
____________________________________________ Space, the final frontier? not any more... All limits henceforth are self-imposed. “libera tute vulgaris ex”
|
|
|
|