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 May 14th, 2007, 12:32 PM
Registered User
 
Join Date: Sep 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default insert from one table to 2nd if row already exists

I have a table that contains employee data pulled from another database. I want to take the data in this table (EmpID, Last_promotion_date) and use it to insert into a 2nd table (EmpID (char), Last_promotion_date (datetime), Emp_row_Number (int)). Using the data from the first table, if a row does not exist in table 2 for a given EmpID, insert a row. However, if a row does exist, where the Last_promotion_date is different from the row coming from table one, insert a new row for that EmpID, with the new Last_promotion_date value, but with a Emp_row_Number value incremented by one over the last row for that EmpID. Anyone have any idea on how to do this with Stored Proc/Triggers?

Thanks in advance
 
Old May 14th, 2007, 03:06 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Just a suggestion. You might want to take a look at the application logic elsewhere. For instance, when a person gets a promotion, on that 'Save' add another row to the second table. That way you always know that row will be in the second table. You also might want to look at why that second table would exist if you already have the data in another table.

 
Old May 15th, 2007, 12:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Code:
INSERT INTO 2ndTable SELECT 1stTable.EmpID, 1stTable.Last_promotion_date, MAX(2ndTable.EmpID) + 1 
    FROM 1stTable, 2ndTable 
    WHERE 1stTable.EmpID <> 2ndTable.EmpID OR 
1stTable.Last_promotion_date <> 2ndTable.Last_promotion_date
I am not sure if you need to use OR or AND operator there, since I am not clear if you wanted to do it as separate inserts or as single insert. I think you should decide on that. Moreover, you didn't mention what should go into Emp_row_Number when EMPID doesn't match between those 2 tables in question, though I used MAX EMPID + 1 for that too.

Hope that helps.
Cheers

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
insert row into table stealthdevil Access VBA 11 June 16th, 2007 08:32 AM
How I insert button into table for select row. oatza ASP.NET 2.0 Basics 5 May 10th, 2006 12:09 AM
Insert New Row into Table (VB 2005) adit9 Visual Basic 2005 Basics 0 March 3rd, 2006 03:25 AM
Problem with insert new row in *.dbo table dimeanel Pro VB.NET 2002/2003 1 January 23rd, 2006 12:05 PM
Insert Row Into Access Table With VBScript ritag Classic ASP Databases 2 August 5th, 2004 08:17 AM





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