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 November 19th, 2004, 12:49 PM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default Insert into 2nd Database if it doesn't exist

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.

*******(*)*******
__________________
*******(*)*******
 
Old November 22nd, 2004, 06:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old November 22nd, 2004, 07:24 PM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default

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!:)

*******(*)*******
 
Old November 22nd, 2004, 07:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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.
Code:
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.
Code:
INSERT INTO ProjectStatus..ProjectInfo2 SELECT COL1, COL2, COL3,... from INSERTED
Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old November 23rd, 2004, 12:36 PM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default

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!:D

*******(*)*******
 
Old November 23rd, 2004, 03:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thats great! Glad the you are through.

Cheers!:)

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert where record doesn't already exist Neal SQL Server 2000 3 February 4th, 2008 10:09 AM
database dont exist zachatcoza All Other Wrox Books 2 July 31st, 2007 02:44 PM
insert from one table to 2nd if row already exists brocktune SQL Server 2000 2 May 15th, 2007 12:56 AM
Can't insert into database chrscote Classic ASP Databases 1 June 15th, 2005 04:07 AM
Deleting tables in Access Database, if they exist mckly Pro VB Databases 1 August 25th, 2004 11:10 PM





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