473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SPROC help for insert into from related records

EJO
with sql 2000 enterprise

Trying to build a stored procedure that will take the rows of a parent
table, insert them into another table as well as the rows from a child
table to insert into another table and be able to maintain the
relationships between the parent/child rows of the new records.
Something like

old_id
new_id
t1 = select field1 from table where id=old_id
t2 = select field1, child_id, id from table

While not t1.eof
with t2
addnew
t2.field1=t1.field1
t2.child_id = new_id
update
t3 = select field1 from table1 where child_id=t1.id
t4 = select field1, child_id from table1
while not t3.eof
with t4
addnew
t4.field1 = t3.field1
t4.child_id = t2.id
update
end
t3.movenext
loop
end
t1.movenext
loop

I need to pass old_id and retrieve new_id and am unsure of the best
method to accomplish the whole thing.
Sep 5 '08 #1
1 2637
ng
On 5 Sep., 18:09, EJO <MyD...@gmail.comwrote:
with sql 2000 enterprise

Trying to build a stored procedure that will take the rows of a parent
table, insert them into another table as well as the rows from a child
table to insert into another table and be able to maintain the
relationships between the parent/child rows of the new records.
Something like

old_id
new_id
t1 = select field1 from table where id=old_id
t2 = select field1, child_id, id from table

While not t1.eof
*with t2
* addnew
* * t2.field1=t1.field1
* * t2.child_id = new_id
* update
* t3 = select field1 from table1 where child_id=t1.id
* t4 = select field1, child_id from table1
* *while not t3.eof
* * with t4
* * * addnew
* * * *t4.field1 = t3.field1
* * * *t4.child_id = t2.id
* * * update
* * end
* * t3.movenext
* * loop
* *end
* t1.movenext
* loop

I need to pass old_id and retrieve new_id and am unsure of the best
method to accomplish the whole thing.

Hi EJO,

I wouldn't youse a cursor. Here's my proposal:

======================
USE master
GO

--create some test tables, delete if scripts runs before
IF NOT OBJECT_ID('t1src') IS NULL BEGIN
DROP TABLE t1src
END
IF NOT OBJECT_ID('t2src') IS NULL BEGIN
DROP TABLE t2src
END
IF NOT OBJECT_ID('t3dst') IS NULL BEGIN
DROP TABLE t3dst
END
IF NOT OBJECT_ID('t4dst') IS NULL BEGIN
DROP TABLE t4dst
END

CREATE TABLE t1src (ID int)
CREATE TABLE t2src (ID int, ID_t1src int, Field1 int)

CREATE TABLE t3dst (ID_New int, ID_Old int)
CREATE TABLE t4dst (ID int, ID_t3dst int, Field1 int)

--insert some dummy data
INSERT INTO t1src (ID) VALUES (100)
INSERT INTO t1src (ID) VALUES (101)
INSERT INTO t1src (ID) VALUES (102)
INSERT INTO t1src (ID) VALUES (103)

INSERT INTO t2src (ID, ID_t1src, Field1) VALUES (1000, 100, 99)
INSERT INTO t2src (ID, ID_t1src, Field1) VALUES (1001, 100, 99)
INSERT INTO t2src (ID, ID_t1src, Field1) VALUES (1002, 100, 99)
INSERT INTO t2src (ID, ID_t1src, Field1) VALUES (1003, 101, 99)
INSERT INTO t2src (ID, ID_t1src, Field1) VALUES (1004, 101, 99)
INSERT INTO t2src (ID, ID_t1src, Field1) VALUES (1005, 102, 99)
INSERT INTO t2src (ID, ID_t1src, Field1) VALUES (1006, 103, 99)
IF NOT OBJECT_ID('usp_copyRows') IS NULL BEGIN
DROP PROCEDURE usp_copyRows
END
GO
--create the procedure
CREATE PROCEDURE usp_copyRows @old_ID int
AS
DECLARE @ID_New int
--select a new id (MAX + 1)
SELECT
@ID_New = (CASE WHEN MAX(ID_New) IS NULL THEN 0 ELSE MAX(ID_New)
END) + 1
FROM t3dst

--copy t1 row
INSERT INTO t3dst
(ID_New
, ID_Old)
SELECT
@ID_New
, ID
FROM t1src
WHERE ID = @old_ID


--copy child rows
INSERT INTO t4dst
(ID,
ID_t3dst,
Field1)
SELECT
ID
, @ID_New
, Field1
FROM t2src
WHERE ID_t1src = @old_ID

--return new id
RETURN @ID_New

GO


--call the proc
DECLARE @IDNew int
EXEC @IDNew = usp_copyRows @old_ID = 100
select @IDNew
GO

--if you do it twice, it works, too.
DECLARE @IDNew int
EXEC @IDNew = usp_copyRows @old_ID = 100
select @IDNew
GO

--SELECT * FROM t1src
--SELECT * FROM t2src

--select the copied data
SELECT * FROM t3dst
SELECT * FROM t4dst

=============


Michael
http://www.sql-server.zankl-it.de




Sep 5 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: adrian | last post by:
hi all this is my first post to this group, so pls bear with me while i try to make some sense. i am trying to create a sproc which uses dynamic sql to target a particuar table eg. '.' and...
12
by: scott | last post by:
In LISTING 2, I have a SPROC that returns a recordset and a recordcount in SQL QA. I can access the Recordset with no problem. How can I grab the Recordcount with ASP code at the same time I'm...
1
by: teddysnips | last post by:
SQL Server 2000 I have a stored procedure that uses an extended SPROC to send an email notification to my customers when a document is distributed. However, the SPROC has an unexpected side...
1
by: billa1972 | last post by:
Hi, I have a sproc with 5 params that takes about 40 seconds to return. But when I Create a Temp table and do a Insert Into #temp Exec sproc param1, param2, param3, param4, param5 it...
4
by: Radu | last post by:
Hi. It seems to be very simple, actually, but I don't know if it is feasible in TSQL. I have a sproc which gathers in one place many calls to different other sprocs, all of them taking a...
3
by: Radu | last post by:
Hi. I have lots of processing to do on the server - from the client (Access) I call a sproc which returns a recordset (the sproc is essentially a big "select"). With the obtained data , I need to...
1
by: P | last post by:
Hello, I am having a difficult time updating a record via a stored procedure using the gridview and sqldatasource. I cannot seem to be able to find a way to set everything up so that I can pass...
4
by: Chad Micheal Lawson via .NET 247 | last post by:
I'm stumped at this point and I'm tired of trying things so I'mposting in hopes of some guru with a sharp eye. I have anasp.net app running on a local Win XP Pro box. Within the app,I call a SPROC...
0
by: seevion | last post by:
My first post (beginner).. I hope it is clear enough and appreciate your taking the time to consider helping. I have an existing sproc that takes a cart transaction from a table and inserts shared...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.