Tuesday, July 28, 2009

SQL Query - Help?

I have the following data in a table T1:





Col1 Col2 Col3 Col4 Col5


10 x 20 a b


10 x 20 a1 b1


10 y 20 c d





Now looking at this table T1, which I am generating dynamically as a temp-table, I want to "insert" data in a different table T2 as rows, in the following fashion:





Single_Column


10 x


20 a b


20 a1 b1


10 y


20 c d





To get you familiarity with the format of the data, this would be more like an XML structure, where "10 x" is the data in the parent-node, and "20 a b", "20 a1 b1" are the two child nodes. But in the table T2 shown above, this child-node data will be in different rows, just below the parent-node data row.





Please advice about the insert statement.





Thanks.

SQL Query - Help?
You can't really do this sort of thing with SQL. Even though the rows may (or may not) be inserted in a certain order, you can't be sure that the rows will maintain this order in the table or be returned in this order when you go to access the data. That's one major thing you have to remember when dealing with databases -- the order of rows is irrelevant and can't be preserved throughout the lifetime of the database.





What you can do (if you're not limiting yourself to one column) is create an ID column for each node, and then another column that specifies which node is the parent of that node. Example:





ID - Data - Parent


---------------------------


1 - 10 x - null


2 - 20 a b - 1


3 - 20 a1 b1 - 1


4 - 10 y - null


5 - 20 c d - 4
Reply:Use Select Into


No comments:

Post a Comment