Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 7th, 2007, 11:50 PM
Registered User
 
Join Date: Mar 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default trigger help plz

hi this is my requirement...
i ve two tables say tab1 and tab2
both have fields say qty1 and qty2
if i update or insert values on tab2.qty2 then
the value from tab1.qty1 should be obtained
and the value inserted or updated should be obtained
and the value tab1.qty1 - value updated or inserted
and then tat value should be updated back to the tab1.qty1
how to do this
help

 
Old March 8th, 2007, 09:26 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Little hard to understand what you are trying to do. May be my brain is clogged at the moment. Can you explain that with some example or a bit elaborate?

Cheers

_________________________
- Vijay G
Strive for Perfection
 
Old March 9th, 2007, 12:32 AM
Registered User
 
Join Date: Mar 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i ve two tables say table1 and table2
table structure:::
table 1
quantity int
table 2
quantity int

if i update quantity in table2 then
the value table1.quantity should be got and then value updated in table2 should be got
both should be summed and then it should update in table1.quantity

clear???

 
Old March 9th, 2007, 01:24 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

First of all I dont understand why you wanted to accomplish it. How many rows are there in Table1 and Table2, does it only have QTY column? Are you updating just one row or multiple rows? If multiple rows are updated in table2, with what reference does the rows in table1 are to be updated?

More confusions this time. Your original post reads value tab1.qty1 - value updated or inserted, the second post reads value table1.quantity should be got and then value updated in table2 should be got
both should be summed Which one you want to do? Addition or Subtraction?

Too many questions???

_________________________
- Vijay G
Strive for Perfection
 
Old March 11th, 2007, 10:54 PM
Registered User
 
Join Date: Mar 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi...
 this my actual table

table 1: tbl1

id
material
quantity

table 2 :

rpaper
rreqd
rused
wid
id

here ...
tabl1.material is the value in tbl2.rpaper
this is my real requiremenett :::
if i insert then:
the newest row inserted
value of tbl2.rused should be subracted from tbl1.quantity and then it should be updated back to tbl1.quantity
it may insert mulitple rows too.
with reference to tabl1.material = tbl2.rpaper (both are equal)

if i update then

then original value of tbl2.rused should be got and added with tbl1.material and then
the updated value of tbl2.rpaper should be got and subracted with
the tbl1.material and updated back to tbl1.material

can ;you help now
 
Old March 12th, 2007, 03:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Code:
CREATE TRIGGER trig_Insert_tbl2 
ON tbl2
FOR INSERT
AS
    UPDATE tbl1 SET tbl1.Quantity = tbl1.Quantity - Inserted.rused
    FROM tbl1, Inserted
    WHERE  tabl1.material =  Inserted.rpaper
    This should be what you are looking for, on the insert operation stuff.

then original value of tbl2.rused should be got and added with tbl1.material and then
the updated value of tbl2.rpaper should be got and subracted with
the tbl1.material and updated back to tbl1.material
On the above statement, I am confused a bit. When you say tabl1.material = tbl2.rpaper I hope tabl1.material has reference to tbl2.rpaper or the other way round. But if the tbl1.material is updated, does it not break the reference? How do you maintain data integrity in doing so? What datatypes are tbl1.material, tbl2.rused and tbl2.rpaper as you do add/subtract involving these three columns?

Can you explain us on that?

_________________________
- Vijay G
Strive for Perfection
 
Old March 12th, 2007, 05:58 AM
Registered User
 
Join Date: Mar 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi.. thnkx for your reply... its working good for insert
but i dnt know how to work with deletion and updation

i tried this for deletion

CREATE TRIGGER trig_delete_tbl2
ON tbl2
FOR delete
AS
    UPDATE tbl1 SET tbl1.Quantity = tbl1.Quantity + Inserted.rused
    FROM tbl1, deleted
    WHERE tabl1.material = deleted.rpaper

it works good but...
when two rows are getting deleted then only only row val is getting updated over the tbl1..
i think i need to put " for each row" over the trigger
but i dnt know how to use this..
plz help me...

how to write update tigger hlep
 
Old March 12th, 2007, 08:07 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

You didn't answer my questions that I asked in my previous post. That should solve the purpose...

Cheers

_________________________
- Vijay G
Strive for Perfection
 
Old March 12th, 2007, 10:07 PM
Registered User
 
Join Date: Mar 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

no it doesnt have the reference.... the values are just equal... tabl1.material = Inserted.rpaper
that should be used for where condition and its not goin to be updated...
only tbl1.quantity should be updated and not other values...








Similar Threads
Thread Thread Starter Forum Replies Last Post
plz.....plz solve out my problem.... kethireddy435 ASP.NET 1.x and 2.0 Application Design 1 October 4th, 2007 12:56 PM
do I need a trigger? Warbird SQL Server 2000 1 April 5th, 2005 08:07 AM
Trigger arshad mahmood C++ Programming 4 June 24th, 2004 07:10 AM
Trigger ! minhtri Pro VB Databases 2 June 23rd, 2004 02:27 AM
Using instead of trigger dmr999 SQL Server 2000 1 November 29th, 2003 02:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.