Click here to Skip to main content
15,913,941 members
Home / Discussions / Database
   

Database

 
AnswerRe: Write to text file from a sql server stored-procedure Pin
flippydeflippydebop30-Oct-06 22:38
flippydeflippydebop30-Oct-06 22:38 
AnswerRe: Write to text file from a sql server stored-procedure [modified] Pin
flippydeflippydebop30-Oct-06 22:40
flippydeflippydebop30-Oct-06 22:40 
Questioninserting values from select query Pin
dj.rock30-Oct-06 19:46
dj.rock30-Oct-06 19:46 
AnswerRe: inserting values from select query Pin
neilsolent31-Oct-06 0:54
neilsolent31-Oct-06 0:54 
QuestionHow to store an 1:N tree (Hierarchy) Pin
peterchen30-Oct-06 14:52
peterchen30-Oct-06 14:52 
AnswerRe: How to store an 1:N tree (Hierarchy) Pin
Colin Angus Mackay31-Oct-06 4:37
Colin Angus Mackay31-Oct-06 4:37 
AnswerRe: How to store an 1:N tree (Hierarchy) Pin
Damodar Periwal31-Oct-06 9:46
Damodar Periwal31-Oct-06 9:46 
AnswerRe: How to store an 1:N tree (Hierarchy) Pin
Eric Dahlvang1-Nov-06 11:31
Eric Dahlvang1-Nov-06 11:31 
IMHO the self referencing table structure you have chosen is well suited for a "Help Contents" tree. As for orphaned cycles, you could write a trigger to delete all descentants, or just write a function that returns a table of all descendants:

Given the following:

TableName=SelfRefTable

Fields=PKey [int] Name [varchar (50)] PKeyOfParent [int]

PKey    Name                                    PKeyOfParent
------------------------------------------------------------
0       Root                                    NULL 
1       Child1                                  0
2       Child2                                  0
3       A GrandChild                            1
4       Another GrandChild                      2
5	A Great GrandChild                      3
6	A Great Great GrandChild                5
7 	One More GrandChild                     3


You can delete Node 3 and all of its descendants with a function call:

delete from SelfRefTable where PKey in(select PID from GetDescendants(3))
This will delete PKey 3,5,6,7 in the above data.

CREATE FUNCTION GetDescendants (@nID int)  
RETURNS @tDescReturn table (PID int)
AS  
begin
DECLARE @RowsAdded int
declare @tDesc   TABLE (PKey int,PKeyOfParent int, processed tinyint default 0) 

INSERT @tDesc
   SELECT PKey,PKeyOfParent,0
   FROM SelfRefTable 
   WHERE PKey = @nID 

SET @RowsAdded = @@rowcount

WHILE @RowsAdded > 0
   BEGIN
	UPDATE @tDesc
      		SET processed = 1
      		WHERE processed = 0

	INSERT @tDesc
	      SELECT e.PKey, e.PKeyOfParent,0
	      FROM SelfRefTable e, @tDesc r
	      WHERE e.PKeyOfParent =r.PKey and r.processed = 1

      UPDATE @tDesc
	      SET processed = 2
	      WHERE processed = 1

      SET @RowsAdded = @@rowcount

   END

 INSERT @tDescReturn
   SELECT PKey 
   FROM @tDesc
return 
end


Also, if you want all ancestors:
CREATE FUNCTION GetAncestors (@nID int)  
RETURNS @tAscReturn table (PID int)
AS  
begin
DECLARE @RowsAdded int
declare @tAsc   TABLE (PKey int,PKeyOfParent int, processed tinyint default 0) 

INSERT @tAsc
   SELECT PKey,PKeyOfParent,0
   FROM SelfRefTable 
   WHERE PKey = @nID 

SET @RowsAdded = @@rowcount

WHILE @RowsAdded > 0
   BEGIN
	UPDATE @tAsc
      		SET processed = 1
      		WHERE processed = 0

	INSERT @tAsc
	      SELECT e.PKey, e.PKeyOfParent,0
	      FROM SelfRefTable e, @tAsc r
	      WHERE e.PKey = r.PKeyOfParent  and r.processed = 1
	
      	UPDATE @tAsc
	      SET processed = 2
	      WHERE processed = 1

	SET @RowsAdded = @@rowcount
   END

 INSERT @tAscReturn
   SELECT PKey 
   FROM @tAsc
return 
end


Transact-SQL Reference:
CREATE FUNCTION[^]
see: Multi-statement table-valued function


--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters

QuestionMicrosoft Access VBA Question About ListBoxes... Pin
new_phoenix30-Oct-06 14:15
new_phoenix30-Oct-06 14:15 
QuestionSQL Select Puzzle Pin
neilsolent30-Oct-06 9:51
neilsolent30-Oct-06 9:51 
AnswerRe: SQL Select Puzzle Pin
Eric Dahlvang31-Oct-06 3:07
Eric Dahlvang31-Oct-06 3:07 
QuestionWhere can I lean about stored procedures? Pin
CCMint30-Oct-06 7:01
CCMint30-Oct-06 7:01 
QuestionCOPY TABLE giving unexpected EOF error Pin
Zeolite30-Oct-06 6:55
Zeolite30-Oct-06 6:55 
QuestionBest way to save data Pin
knappster30-Oct-06 1:43
knappster30-Oct-06 1:43 
QuestionADO.NET related question Pin
Imtiaz Murtaza30-Oct-06 0:57
Imtiaz Murtaza30-Oct-06 0:57 
AnswerRe: ADO.NET related question Pin
Colin Angus Mackay30-Oct-06 1:54
Colin Angus Mackay30-Oct-06 1:54 
GeneralRe: ADO.NET related question Pin
Damodar Periwal31-Oct-06 10:14
Damodar Periwal31-Oct-06 10:14 
AnswerRe: ADO.NET related question Pin
Hunuman30-Oct-06 2:02
Hunuman30-Oct-06 2:02 
GeneralData Type Issue in Where Clause Pin
Brady Kelly30-Oct-06 0:13
Brady Kelly30-Oct-06 0:13 
JokeRe: Data Type Issue in Where Clause Pin
Rob Graham30-Oct-06 3:14
Rob Graham30-Oct-06 3:14 
Questionselect query filling 2 typed datasets Pin
steve_rm29-Oct-06 22:10
steve_rm29-Oct-06 22:10 
QuestionPurpose of SQL Server Jobs Pin
King Shez29-Oct-06 19:50
King Shez29-Oct-06 19:50 
AnswerRe: Purpose of SQL Server Jobs Pin
Eric Dahlvang30-Oct-06 3:13
Eric Dahlvang30-Oct-06 3:13 
Questionconnection cannot be made? Pin
Haoman1729-Oct-06 0:48
Haoman1729-Oct-06 0:48 
Questionconnecting to SQL express through ADO Pin
psasidisrcum28-Oct-06 11:54
psasidisrcum28-Oct-06 11:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.