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
 General SQL Server Forums
 New to SQL Server Programming
 UD scalar function

Author  Topic 

Jayeeta Mitra
Starting Member

11 Posts

Posted - 2008-03-05 : 06:32:33
I want to add four fields of a table and place the toatal in a new field.Also I wanna have the average of the fields.

For e.g
I have created a marksheet having four subjects.Now I wanna add the subjects and find the average of the subjects and place them in two different fields in the same table along with the respective names in the table.Pls help.

Thanks in advance.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-03-05 : 06:34:54
[code]UPDATE YourTable
SET SubjectTotal=Sub1+Sub2+Sub3+Sub4,
SubAvg=(Sub1+Sub2+Sub3+Sub4)/4[/code]
Go to Top of Page

Jayeeta Mitra
Starting Member

11 Posts

Posted - 2008-03-05 : 06:36:15
I want to do this through functions.
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-03-05 : 06:37:45
[code]declare @t table
(
id int identity(1,1),
s1 int,
s2 int,
s3 int
)


insert @t
select 26, 78, 57 union all
select 23,95,75 union all
select 17,69,63

select id, (s1+s2+s3) as tot, (s1+s2+s3)/3 as av
from @t[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-03-05 : 06:38:21
You can't update a physical table through UDF.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-03-05 : 06:38:22
why?

also, i guess you actually mean a procedure rather than a function?

Em
Go to Top of Page

Jayeeta Mitra
Starting Member

11 Posts

Posted - 2008-03-05 : 06:40:59
I wanna make this with both procedure and function
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-03-05 : 06:45:14
Why? Because your teacher asked you to do it as a homework?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-03-05 : 06:48:21
both? what do want to acheive with a function though? you just need a procedure to perform the update statement that visakh gave you

Em
Go to Top of Page
   

- Advertisement -