Subject: Insert into 2nd Database if it doesn't exist
Posted By: Arsi Post Date: 11/19/2004 11:49:58 AM
Hello,

I got some help with the update when I was working on that part, but now I need to add some more to the trigger and I've running into a lot of problems. Most of them I've fixed, but now I'm stuck. If anyone can help me I would be very thankful.

Here it is:

ALTER    TRIGGER [Update_DB] ON [dbo].[ProjectInfo]
FOR INSERT, UPDATE
AS
If UPDATE(ProjectName)
IF EXISTS (SELECT * FROM INSERTED)
    UPDATE ProjectStatus..ProjectInfo2
    SET ProjectName = i.ProjectName
    FROM DELETED d, ProjectStatus..ProjectInfo2 t, INSERTED i
    WHERE d.ProjectName = t.ProjectName
ELSE
INSERT INTO ProjectStatus..ProjectInfo2
VALUES ('BidNum', 'ProjectName', 'ProjectLead', 'WklyTotalHrs', 'TotalHrs', 'EmpNum')

Ok. I'm trying to update a record in Table ProjectInfo2 in the ProjectStatus database if an update occurs to Table ProjectInfo in the dbTimeSheet database. Also, it has to check to see if the record exists (mainly the ProjectName) in Table ProjectInfo2 if it does then update, else insert. I am not receiving any errors, but when I there is an update, it does not do the insert if it does not exist.

'BidNum', 'ProjectName'...etc are actually column names in the ProjectInfo2 Table. I am so lost.

*******(*)*******
Reply By: happygv Reply Date: 11/22/2004 5:45:10 PM
Can you post the structure of both the tables(on which the UPDATE happens AND onto which the UPDATE or INSERT has to happen)? That should clear some of the doubts I have in my mind.

Cheers!

_________________________
- Vijay G
Strive for Perfection
Reply By: Arsi Reply Date: 11/22/2004 6:24:43 PM
Sorry I can be confusing even to myself. When db1; Table ProjectInfo gets updated or has a new record inserted, then it should trigger the second database to either insert the new record or update an existing one. The second database is ProjectStatus and has only one table ProjectInfo2. The second Database has only one table. The other is a Microsoft Sharepoint Portal database.

-Thanks Again!

*******(*)*******
Reply By: happygv Reply Date: 11/22/2004 6:42:06 PM
Yes I understand that. But I would like to see the structure of both ProjectInfo and ProjectInfo2 tables. If they both are similar, you may use the INSERTED table's values as such to insert into the ProjectInfo2.
INSERT INTO ProjectStatus..ProjectInfo2 SELECT * from INSERTED
If the sequence of columns in PROJECTINFO2 are not the same as that of the PROJECTINFO, then you may sequence it within the SELECT statement given above.
INSERT INTO ProjectStatus..ProjectInfo2 SELECT COL1, COL2, COL3,... from INSERTED
Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
Reply By: Arsi Reply Date: 11/23/2004 11:36:58 AM
OMG! I understand now! Thank you so very much! This is very exciting! I am now using the second part of the sample you gave me and its now functioning wonderfully! Thank you for all your help!

*******(*)*******
Reply By: happygv Reply Date: 11/23/2004 2:06:20 PM
Thats great! Glad the you are through.

Cheers!

_________________________
- Vijay G
Strive for Perfection

Go to topic 22535

Return to index page 708
Return to index page 707
Return to index page 706
Return to index page 705
Return to index page 704
Return to index page 703
Return to index page 702
Return to index page 701
Return to index page 700
Return to index page 699