Click here to Skip to main content
15,897,187 members
Home / Discussions / Database
   

Database

 
AnswerRe: Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. Pin
liviucatrina13-Apr-10 4:24
liviucatrina13-Apr-10 4:24 
GeneralSQL IF statement Pin
DBLearner15-Apr-09 8:31
DBLearner15-Apr-09 8:31 
GeneralRe: SQL IF statement Pin
Luc 64801115-Apr-09 8:47
Luc 64801115-Apr-09 8:47 
GeneralRe: SQL IF statement Pin
DBLearner15-Apr-09 9:00
DBLearner15-Apr-09 9:00 
GeneralRe: SQL IF statement Pin
Luc 64801115-Apr-09 9:02
Luc 64801115-Apr-09 9:02 
GeneralRe: SQL IF statement Pin
Ashfield15-Apr-09 9:47
Ashfield15-Apr-09 9:47 
GeneralRe: SQL IF statement Pin
DBLearner17-Apr-09 1:08
DBLearner17-Apr-09 1:08 
GeneralRe: SQL IF statement Pin
Ashfield17-Apr-09 1:49
Ashfield17-Apr-09 1:49 
QuestionAny shorter way to total this? Pin
Muammar©14-Apr-09 21:10
Muammar©14-Apr-09 21:10 
AnswerRe: Any shorter way to total this? Pin
Giorgi Dalakishvili14-Apr-09 21:30
mentorGiorgi Dalakishvili14-Apr-09 21:30 
GeneralRe: Any shorter way to total this? Pin
Muammar©15-Apr-09 0:03
Muammar©15-Apr-09 0:03 
GeneralRe: Any shorter way to total this? Pin
Eddy Vluggen15-Apr-09 0:28
professionalEddy Vluggen15-Apr-09 0:28 
GeneralRe: Any shorter way to total this? Pin
Muammar©15-Apr-09 0:44
Muammar©15-Apr-09 0:44 
AnswerRe: Any shorter way to total this? [modified] Pin
Eddy Vluggen15-Apr-09 2:50
professionalEddy Vluggen15-Apr-09 2:50 
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 

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.