Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 February 7th, 2007, 01:45 AM
Authorized User
 
Join Date: Nov 2005
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trigger update problem (urgent)

hi all,
am new 4 this trigger db object. i have to update two tables.
This is my problem.
User click update button then its updating in my Supplier master table(Supp_Name,Supp_Add,Supp_mail,Supp_phone).its ok. that same time i need to update that data to Contacts table(ContactName,ContactAdd,ContactMail,CantactPh one).
So how i can do it.
Can u help me to solve this problem. But i didnt use this triggers b4. im new to this db object.
thanks
__________________
MSB
 
Old February 7th, 2007, 06:31 AM
Authorized User
 
Join Date: Sep 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to prabodh_mishra
Default

You need a primary key or unique field for updation of the table.
Can you tell me how you define uniqueness of each row?

Cheers,
Prabodh
 
Old February 7th, 2007, 10:18 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

You don't need a trigger for this... prabodh is correct... just do two updates from the app. In order to do that, you need to know what the primary key of each table is so your code can narrow the update down to a single row.

--Jeff Moden
 
Old February 10th, 2007, 03:12 AM
Authorized User
 
Join Date: Nov 2005
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes . i didnt wrote it. Specially have to tell There have another table .tblContact. It has ContactId,SUPP_CODE.
This is a table link.
tblSupplier INNER JOIN tblContacts ON
tblSupplier.ContactId =tblContacts.ContactId INNER JOIN
SUPPLIER_MASTER ON tblSupplier.SUPP_CODE =SUPPLIER_MASTER.SUPP_CODE


Sorry. i didnt sent those.
can u explain it again
thanks Prabodh
 
Old February 10th, 2007, 03:17 AM
Authorized User
 
Join Date: Nov 2005
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Jeff Moden
 You don't need a trigger for this... prabodh is correct... just do two updates from the app. In order to do that, you need to know what the primary key of each table is so your code can narrow the update down to a single row.

--Jeff Moden
No Jeff. I think i have to use Trigger. Cause of There My SupplierMaster table data load from Factory. I didnt do any to that table. but im using there datas. that is a problem.
 
Old February 10th, 2007, 09:15 AM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Based on the limited information above, it looks like you have two choices. We need to know the relationship between the two tables, Supplier and Contact. We can make assumptions, but they might be wrong. However, based on the information above, it looks like you can do one of the following:

1) You can do two updates from the application like Jeff suggests. This is can be done by inserting first into the primary table, grabbing the newly created ID, and then using that to insert into the second table.

2) A trigger will also work. Triggers can be created by using the CREATE TRIGGER statement. Look up Triggers in BOL and they will walk you through creating triggers. There are three types, INSERT, UPDATE, and DELETE. The general syntax is as follows:

CREATE TRIGGER <name>
ON <tablename>
AFTER INSERT
AS
  //do stuff
GO

Hope this helps...

Scott Klein
Author - Professional SQL Server 2005 XML
http://www.wrox.com/WileyCDA/WroxTit...764597922.html
 
Old February 26th, 2007, 02:39 AM
Authorized User
 
Join Date: Sep 2003
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to prabodh_mishra
Default

Sorry for belated response. I was little busy off-late.
If above responses haven't helped much then below is sample trigger that will synchronize terr_copy table with territories. You can amend it as per your need.


CREATE TRIGGER tr1 ON [dbo].[Territories]
FOR INSERT, UPDATE, DELETE
AS
Begin
-- Update or delete record.
If Exists(select * from deleted)
Begin
  If Exists(select * from Inserted)
      Update terr_copy
         SET id = TerritoryId,
    detail = TerritoryDescription,
    x = RegionId
    from terr_copy inner join inserted on id = TerritoryId
    where id = territoryid
  Else
       Delete terr_copy
    from terr_copy inner join deleted on id = TerritoryId
    where id = territoryid
End
-- Add NEW record.
else
      INSERT INTO terr_copy
         SELECT * FROM inserted

End




Cheers,
Prabodh





Similar Threads
Thread Thread Starter Forum Replies Last Post
After Update Trigger debbiecoates SQL Server 2000 2 February 15th, 2008 04:55 AM
Create trigger on update bvpsekhar MySQL 23 May 4th, 2007 01:01 AM
insert,update trigger rolle SQL Server 2000 3 September 29th, 2005 08:41 PM
Problem with update trigger gbrown SQL Language 2 September 4th, 2004 12:33 PM
Insert Update Trigger mstuart60 SQL Server 2000 10 September 30th, 2003 06:54 AM





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