Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Create a function for the default value

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-03-05 : 09:00:06
Steven Pinto writes "I created a field called Total which I want populated with the value Field1/Field2. If Field1 is 0 then it's left blank else it does the calculation. Should I use a user-defined function for this then place that function in the default value for the field Total? If so how do I go about it."

Nazim
A custom title

1408 Posts

Posted - 2002-03-05 : 09:21:01
a simple update statement will do .
you can call it via a stored procedure. if you want the calculation to be done on every insert of a record you can write a trigger too.


Create procedure Upd_field
as
update tablename field1=field1/field2 where field1 <> 0


HTH

--------------------------------------------------------------
Go to Top of Page

Jay99

468 Posts

Posted - 2002-03-05 : 09:38:41
alternatively, make total a computed column


create table StevenPinto (
Field1 numeric,
Field2 numeric,
Total as (case when field1 <> 0 then field1/field2 else null end)
)


Jay
Go to Top of Page

Lou
Yak Posting Veteran

59 Posts

Posted - 2002-03-05 : 13:40:03
My personal preference for preventing division by zero, is to wrap the denominator with "nullif(denominator expression,0)". If the denominator is zero, it will be changed to null. If you divide by null, the whole expression is null, which is what I want.

Go to Top of Page
   

- Advertisement -