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 6th, 2007, 07:07 AM
Authorized User
 
Join Date: Sep 2004
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Insert data from TableA to TableB

I have the following fields in TableA running on SQL Server 2000:

Dates as datetime
Item_ID as int (Primary key)
Item_No as int
Qty as numeric
Unit_Price as numeric

TableA consist of the following info :
======================================
 Dates | Item_ID | Item_No | Qty | Unit_Price
======================================
30/10/2007 | IT1000 | 1234 | 2 | 4
======================================
30/10/2007 | IT1001 | 1235 | 2 | 6
======================================
28/09/2007 | IT1002 | 1236 | 4 | 8
======================================
01/11/2007 | IT1003 | 1235 | 2 | 2

I have to assign 3 units of Item_No 1235 from TableA into TableB, with the following rules :

1) assign units from the earliest date or earliest Item_ID to Qty field in TableB
           => 2 units from Item_ID : IT1001 insert to Qty field in TableB
           => Unit_Price of 6 from Item_ID : IT1001 insert to Unit_Price field in TableB

2) assign the balance of 1 unit from the next earliest date or earliest Item_ID to Qty field in TableB
           => 1 unit from Item_ID : IT1003 insert to Qty field in TableB
           => Unit_Price of 2 from Item_ID : IT1003 insert to Unit_Price field in TableB

3) Update Qty field in TableA after inserting the above quantity for Item_No 1235 into TableB

Note:sql function should be intelligent to assign the units required base on earliest date or
       earliest Item_ID automatically. If units required is insufficient, it should be able to loop
       for the next available units, if the units to assign is greater than the units available in TableA,
       an error message should be printed, telling the user, units to assign is insufficient. If Item_No to be
       assigned does not exist in TableA, an error message should be printed, telling the user, item not exist.

INSERT Result in TableB should consist the following info :

======================================
 Dates | Item_ID | Item_No | Qty | Unit_Price
======================================
30/10/2007 | IT1001 | 1235 | 2 | 6
======================================
01/11/2007 | IT1003 | 1235 | 1 | 2

UPDATE Result in TableA should consist the following info :

======================================
 Dates | Item_ID | Item_No | Qty | Unit_Price
======================================
30/10/2007 | IT1000 | 1234 | 2 | 4
======================================
30/10/2007 | IT1001 | 1235 | 0 | 6
======================================
28/09/2007 | IT1002 | 1236 | 4 | 8
======================================
01/11/2007 | IT1003 | 1235 | 1 | 2

Can a sql function be created for the above action? Thanks guys!







Similar Threads
Thread Thread Starter Forum Replies Last Post
insert same data to another remo MySQL 2 July 20th, 2007 12:08 AM
Data is not refreshed after an insert TomTab ADO.NET 3 January 19th, 2006 12:24 AM
Help Please with my code to insert data shopgirl Classic ASP Databases 7 February 24th, 2004 01:58 AM





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