SQL Server Performance

sum function

Discussion in 'General DBA Questions' started by Leite33, Dec 17, 2006.

  1. Leite33 New Member

    Hi i want to create a function to sum my two fields of table 'fields' row by row.
    Because i am new on this i made something below but is totally wrong. Can anyone help me and correct it???





    USE fields
    IF EXISTS (SELECT * from Fields)

    GO

    CREATE FUNCTION compare (@FieldA,@FieldB)
    Returns Result int
    As begin Return (FieldA+FieldB)
    End


  2. madhuottapalam New Member


    Hi

    Just i hope this is what u want

    U have a Employee Table something like this

    EmpID BasicSalary DA

    A001 1000 200
    B002 2000 500
    C003 5000 800

    U need this

    EmpID BasicSalary DA TotalSalary

    A001 1000 200 1200
    B002 2000 500 2500
    C003 5000 800 5800

    there is no need of any function here

    this query will give u the result

    select EmpID,BasicSalary,Da,BasicSalary+DA as TotalSalary from Employee

    Madhu

  3. FrankKalis Moderator

    Actually there is no reason for using a function here. As Madhu showed you, the SUM can and should be, easily derived at runtime. Either in the SELECT statement or even in the front-end. If, for some reason, you insist of "having" it in the database you might want to check out "Computed columns" in BOL.

    --
    Frank Kalis
    Moderator
    Microsoft SQL Server MVP
    Webmaster:http://www.insidesql.de
  4. Madhivanan Moderator

    Leite33, you seem to be FUNCTION lover. As suggested in other thread you dont need actually a Function. Create a procedure with the code. Also if you use front end application, do concatenation there

    Madhivanan

    Failing to plan is Planning to fail

Share This Page