473,623 Members | 2,433 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert into a table recursively

I have a Table that contains Items of the Type "Step". The primary key
is "StepID". Each step can have have a target step, wich represents a
subsequent step. So I have a Foreign key relationship within the same
table:

Primary Key: StepID --> Foreign Key: TargetStepID

Now if I want to insert a group of new Steps into the table, I can only
insert steps whose target step is already insterted to the table. How
would I solve this problem ? Do I need to write my own client side (I
am using ADO.NET / C#) that loops through the targetsteps and inserts
the targetsteps first ? Or is there a more celver way to do this ?

May 19 '06 #1
4 5089
Robert Ludig (sc************ ******@gmx.de) writes:
I have a Table that contains Items of the Type "Step". The primary key
is "StepID". Each step can have have a target step, wich represents a
subsequent step. So I have a Foreign key relationship within the same
table:

Primary Key: StepID --> Foreign Key: TargetStepID

Now if I want to insert a group of new Steps into the table, I can only
insert steps whose target step is already insterted to the table. How
would I solve this problem ? Do I need to write my own client side (I
am using ADO.NET / C#) that loops through the targetsteps and inserts
the targetsteps first ? Or is there a more celver way to do this ?


The easiest is of course to insert all at once:

CREATE TABLE rekursiv (a int NOT NULL PRIMARY KEY,
b int NULL REFERENCES rekursiv(a))
go
INSERT rekursiv(a, b)
SELECT 1, NULL
UNION
SELECT 10, 1
UNION
SELECT 20, 10
go
SELECT * FROM rekursiv
go
DROP TABLE rekursiv

If the StepID column has the IDENTITY property it becomes more difficult.
The remedy is to remove the IDENTITY property.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 19 '06 #2
I am using C# and ADO.NET to acess the database. I have a Hashtable
that contains all the Steps wich should be insterted (100-10000). I
suppose it is a bad idea to create one big instertion command (as
string) and the do a OleDbCommand.Ex ecuteNonQuery() ? How can I insert
all at once in a clever way by using ADO.NET ?

May 19 '06 #3
Robert Ludig (sc************ ******@gmx.de) writes:
I am using C# and ADO.NET to acess the database. I have a Hashtable
that contains all the Steps wich should be insterted (100-10000). I
suppose it is a bad idea to create one big instertion command (as
string) and the do a OleDbCommand.Ex ecuteNonQuery() ? How can I insert
all at once in a clever way by using ADO.NET ?


That's not bad at all. Most of all it is effecient. If you insert rows
one by one, there will be many network roundtrips.

The one thing that is messy is to pass all the parameters. Of course,
you can build an SQL statement with the parameters expanded, but generally
you should stick to parameterised commands. It's not that difficult to
handle. You build the SQL statement in a loop, and add the SELECT for one
step, and then you add the parameters for that step as well.
The alternative is build an XML document and pass this to a stored
procedure, and then unpack the XML with OPENXML.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 19 '06 #4
Some DDL and sample would help. I sounds vaguely like a graph problem.

May 19 '06 #5

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

Similar topics

14
4284
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to salvage the records from the many table and without going into detail, one of the reasons I can't do the opposite as there are records in the ONE table that I need to keep even if they don't have any child records in the MANY table. Below I created...
16
17003
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
4
5472
by: Chris Kratz | last post by:
Hello all, We have run into what appears to be a problem with rules and subselects in postgres 7.4.1. We have boiled it down to the following test case. If anyone has any thoughts as to why this would be happening, we would appreciate feedback. We have tested on 7.3.4, 7.3.6 and 7.4.1 and all exhibit the same behavior. Test case one tries to populate table2 from table1 with records that are not in table2 already. Table2 gets...
2
3189
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request : INSERT INTO temp_tab VALUES (1,2,3)
2
4180
by: mirandacascade | last post by:
O/S: Win2K Vsn of Python: 2.4 Example: <a> <b createAnotherWhenCondition="x"> <c>text for c</c> <d>text for d</d> </b>
1
12880
by: sudhendra | last post by:
I have a table supertask; which has 24 rows. The table as a primary_key called id. I would like just recursively copy rows into same table. supertask_id is auto generated. So All I need to do is something like insert all into supertask (name,desc,rstrnum) select name, desc, rstrnum from supertask;
6
3447
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am using MS-Access 2000 database table for this app. Note that the datatype of all the fields mentioned above are Text. Apart from the above columns, there's another column in the DB table named 'RegDateTime' whose datatype is Date/Time which is...
6
3712
by: lenygold via DBMonster.com | last post by:
Hi everybody: What is the best way to I have 10 tables with similar INSERT requiremnts. INSERT INTO ACSB.VAATAFAE WITH AA(AA_TIN, AA_FILE_SOURCE_CD, .AA_TIN_TYP) AS ( SELECT AA_TIN, AA_FILE_SOURCE_CD, .AA_TIN_TYP FROM VAATAFAA WHERE AB_TP_ACNT_STAT_CD <0),
1
2645
by: EJO | last post by:
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
0
8165
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8670
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8613
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8469
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6106
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4164
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.