Click here to Skip to main content
15,887,746 members
Home / Discussions / Database
   

Database

 
GeneralRe: Any shorter way to total this? Pin
Muammar©15-Apr-09 10:36
Muammar©15-Apr-09 10:36 
AnswerRe: Any shorter way to total this? Pin
Mycroft Holmes15-Apr-09 1:21
professionalMycroft Holmes15-Apr-09 1:21 
GeneralRe: Any shorter way to total this? Pin
Muammar©15-Apr-09 10:30
Muammar©15-Apr-09 10:30 
AnswerRe: Any shorter way to total this? Pin
i.j.russell15-Apr-09 9:06
i.j.russell15-Apr-09 9:06 
GeneralRe: Any shorter way to total this? Pin
Muammar©15-Apr-09 10:30
Muammar©15-Apr-09 10:30 
QuestionRewrite recursive proc so we can do this in UDF instead? (and more efficient) Pin
devvvy14-Apr-09 18:02
devvvy14-Apr-09 18:02 
AnswerRe: Rewrite recursive proc so we can do this in UDF instead? (and more efficient) Pin
Ashfield14-Apr-09 21:00
Ashfield14-Apr-09 21:00 
GeneralGot recursive CTE sql but... two more questions. Pin
devvvy15-Apr-09 0:07
devvvy15-Apr-09 0:07 
thanks I got my recursive CTE expression working but two more questions!

is it good with UDF (user defined function)? and performance better than CURSOR?

NOTE: You can't do these from UDF:
* execute/sp_executesql to run another stored proc
* use table variable as input/output function parameter
* access temp table

Anyway I got it working (but with problem and corresponding cheats to get around them)

<br />
CREATE TABLE [dbo].[HierarchyMap] (<br />
	[Id] [bigint] IDENTITY (1, 1) NOT NULL,<br />
	[ParentId] [bigint] NOT NULL, <br />
	[ChildId] [bigint] NOT NULL,<br />
	[ParentType] [varchar] (255) NOT NULL,<br />
	[ChildType] [varchar] (255) NOT NULL, <br />
	CONSTRAINT [PK_HierarchyMap] PRIMARY KEY CLUSTERED <br />
	(<br />
		[Id] ASC<br />
	)<br />
)<br />


Assuming I only have two rows in the table - select * from HierarchyMap:
<br />
Id      ParentId ChildId  ParentType    ChildType<br />
-----------------------------------------------------------<br />
1	1	 1	  Group	        SystemUser<br />
2	1	 2	  Group	        SystemUser<br />


Now, my CTE sql is as follows and the problems are:
1. Max depth 100 for recursion exceeded
2. Duplicate rows (no idea..)
<br />
WITH Children AS<br />
(<br />
--initialization<br />
SELECT<br />
	[Id],<br />
	[ParentId],<br />
	[ChildId],<br />
	[ParentType],<br />
	[ChildType]<br />
FROM HierarchyMap<br />
WHERE <br />
	ParentId=1 AND ParentType='Group'<br />
UNION ALL<br />
--recursive execution<br />
SELECT <br />
	[MAP].[Id],<br />
	[MAP].[ParentId],<br />
	[MAP].[ChildId],<br />
	[MAP].[ParentType],<br />
	[MAP].[ChildType],<br />
	CH.Depth+1 'Depth'<br />
FROM HierarchyMap MAP INNER JOIN Children CH<br />
ON MAP.ParentId = CH.ChildId<br />
WHERE <br />
Depth<100          -- PROBLEM 1: If I don't limit depth I get error <b>"The maximum recursion 100 has been exhausted before statement completion."</b><br />
)<br />
SELECT ParentId, ParentType, ChildId, ChildType FROM Children <b>GROUP BY Id, ParentId, ParentType, ChildId, ChildType</b> -- PROBLEM 2: I don't ... understand the duplicate rows... which is why I need to do a GROUP-BY... think I did something wrong?<br />


Suggestion?


http://www.mssqltips.com/tip.asp?tip=1520
http://www.setfocus.com/TechnicalArticles/sql-server-2005-tsql-3.aspx
http://stackoverflow.com/questions/634971/sql-server-how-to-limit-cte-recursion-to-rows-just-recursivly-added
[^]

dev

GeneralRe: Got recursive CTE sql but... two more questions. Pin
Giorgi Dalakishvili15-Apr-09 1:13
mentorGiorgi Dalakishvili15-Apr-09 1:13 
GeneralRe: Got recursive CTE sql but... two more questions. Pin
i.j.russell15-Apr-09 9:10
i.j.russell15-Apr-09 9:10 
GeneralRe: Got recursive CTE sql but... two more questions. Pin
devvvy15-Apr-09 13:25
devvvy15-Apr-09 13:25 
QuestionTABLE VALUE FUNCTION for MySQL? Pin
devvvy14-Apr-09 16:20
devvvy14-Apr-09 16:20 
QuestionFile logging. Pin
Ivan200914-Apr-09 8:50
Ivan200914-Apr-09 8:50 
AnswerRe: File logging. Pin
Ashfield14-Apr-09 21:03
Ashfield14-Apr-09 21:03 
GeneralRe: File logging. Pin
Ivan200915-Apr-09 6:09
Ivan200915-Apr-09 6:09 
QuestionQuery Pin
CodingYoshi14-Apr-09 6:23
CodingYoshi14-Apr-09 6:23 
AnswerRe: Query Pin
Eddy Vluggen14-Apr-09 10:10
professionalEddy Vluggen14-Apr-09 10:10 
Questiondatacolumn boxing Pin
Maverickcool13-Apr-09 22:02
Maverickcool13-Apr-09 22:02 
AnswerRe: datacolumn boxing Pin
Mycroft Holmes14-Apr-09 17:05
professionalMycroft Holmes14-Apr-09 17:05 
GeneralRe: datacolumn boxing Pin
Maverickcool14-Apr-09 23:55
Maverickcool14-Apr-09 23:55 
GeneralRe: datacolumn boxing Pin
Mycroft Holmes15-Apr-09 1:18
professionalMycroft Holmes15-Apr-09 1:18 
AnswerRe: datacolumn boxing Pin
jai_10124-Apr-09 21:49
jai_10124-Apr-09 21:49 
Questionmultiline dynamic tsql - recognize line break? Pin
devvvy13-Apr-09 20:22
devvvy13-Apr-09 20:22 
AnswerRe: multiline dynamic tsql - recognize line break? Pin
devvvy13-Apr-09 20:46
devvvy13-Apr-09 20:46 
QuestionDifference between datatype uniqueidentifier and int for a primary key? Pin
pzn3xq13-Apr-09 10:07
pzn3xq13-Apr-09 10:07 

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.