Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Insert data from TableA to TableB

Author  Topic 

fdtoo
Starting Member

28 Posts

Posted - 2007-11-06 : 07:32:12
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 :

assuming 3 units is a variable declared in sql stored procedure

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 stored procedure 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 stored procedure be created for the above action? Thanks guys!

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2007-11-06 : 09:23:39
This smacks of homework!

What do you have so far? let's see your code and we can help point the way.

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

csundaresh
Starting Member

5 Posts

Posted - 2007-11-06 : 09:59:23
quote:
Originally posted by fdtoo

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 :

assuming 3 units is a variable declared in sql stored procedure

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 stored procedure 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 stored procedure be created for the above action? Thanks guys!




Sure you can. You can refer any number of books or SQL resources online.
Go to Top of Page
   

- Advertisement -