Sql Help

  • I have a table in sql with these field id,item,cost,quantity,total and i want that whenever some new record entered it automatically fill the toal field with value=cost*quantity or total=cost*quantity how can I do this somebody suggest me to use triggers but i don't know about triggers please help

    Thanks

  • I would use a computed column for that.

    create table #sales

     ([id] int,

     item varchar(30),

     cost money,

     quantity int,

     total as cost * quantity)

    insert #sales

     values (1, 'widget', 1.99, 6)

    select * from #sales

    -- Steve

  • Thanks Steve that's what i am looking for.

  • If the computed column were not the last column in the table and a table insert is performed, then must the insert specify the table column names.

    i.e. table cols are :

    id, item, cost, computed column, qty.

    Then does following insert work?

    insert tablename values (1,'widget',1.99,,6)

    Thanks

    Gary Andrews

     

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply