Click here to Skip to main content
15,896,496 members
Home / Discussions / Database
   

Database

 
GeneralSQL DTS Pin
vrkanth11-Jul-05 14:21
vrkanth11-Jul-05 14:21 
GeneralODBC Dialog Pin
Jmuwanga11-Jul-05 1:08
sussJmuwanga11-Jul-05 1:08 
GeneralURGENT:- ODBC connection string problem to access a remote SQL server Pin
Zalak10-Jul-05 20:02
Zalak10-Jul-05 20:02 
GeneralRe: URGENT:- ODBC connection string problem to access a remote SQL server Pin
Albert Pascual11-Jul-05 9:17
sitebuilderAlbert Pascual11-Jul-05 9:17 
GeneralXML Output spread over multiple rows Pin
totig10-Jul-05 19:41
totig10-Jul-05 19:41 
GeneralSqlDmo Pin
mahakhalid10-Jul-05 0:16
mahakhalid10-Jul-05 0:16 
GeneralRe: SqlDmo Pin
Mbat11-Jul-05 3:31
Mbat11-Jul-05 3:31 
Generalsql query Pin
Anonymous9-Jul-05 21:29
Anonymous9-Jul-05 21:29 
GeneralRe: sql query Pin
Colin Angus Mackay9-Jul-05 21:34
Colin Angus Mackay9-Jul-05 21:34 
GeneralDatabase project deployment advice Pin
Kunal Chaudhary9-Jul-05 9:08
Kunal Chaudhary9-Jul-05 9:08 
GeneralRe: Database project deployment advice Pin
Anonymous9-Jul-05 23:30
Anonymous9-Jul-05 23:30 
QuestionHow to check on Column value Pin
gharryh9-Jul-05 5:42
gharryh9-Jul-05 5:42 
GeneralInsert, Update and Delete Problem. Need Help. Thanks. Pin
shapper8-Jul-05 7:34
shapper8-Jul-05 7:34 
GeneralRe: Insert, Update and Delete Problem. Need Help. Thanks. Pin
Rob Graham8-Jul-05 8:08
Rob Graham8-Jul-05 8:08 
GeneralRe: Insert, Update and Delete Problem. Need Help. Thanks. Pin
shapper8-Jul-05 12:16
shapper8-Jul-05 12:16 
GeneralRe: Insert, Update and Delete Problem. Need Help. Thanks. Pin
Rob Graham8-Jul-05 17:16
Rob Graham8-Jul-05 17:16 
GeneralRe: Insert, Update and Delete Problem. Need Help. Thanks. Pin
jjrdk8-Jul-05 10:18
jjrdk8-Jul-05 10:18 
GeneralRe: Insert, Update and Delete Problem. Need Help. Thanks. Pin
Luis Alonso Ramos9-Jul-05 23:26
Luis Alonso Ramos9-Jul-05 23:26 
GeneralSQL Server; Recursive Functions and Scope. (Long-ish post) Pin
Alsvha8-Jul-05 3:45
Alsvha8-Jul-05 3:45 
Hiya all.
I really hope you guys can help me with this question/problem.
First off - sorry for the lenght of the post. Smile | :)

I have a productstructure which I'm trying to read out via SQL, using User Defined Functions.
Setting:
Basically - my structure in the database consists of product groups and products. A product can be sub-group to the parent, and have products connected.
So I have a Group A, which is a parent to Group A-1 which contains the product A-1p1, A-1p2 etc. (simplified, I think currently there is only 7 parent groups, which have a number of subgroups, which have yet another bunch of subgroups which have products connected them)

I use a function which returns a table of groups, given the parents id, so parent id = 0 will return all the root elements.
I also have a function which returns a table of products given the group id.

Then I have created this recursive function:

<small>CREATE FUNCTION fnGroupListByParentRecusive<br />
(<br />
	@CurrentGroupID int,<br />
	@NodePath nvarchar(2000),<br />
	@LanguageCode nvarchar(5)<br />
)<br />
RETURNS @ProductStruct TABLE(NodePath nvarchar(2000), PROD_GroupID int, PROD_GroupName nvarchar(200), PROD_ProductID int, PROD_ProductName nvarchar(200))<br />
AS  	<br />
BEGIN <br />
<br />
-- DECLARE LOCAL VARIABLES --<br />
DECLARE @GroupID INT<br />
DECLARE @Group TABLE(PROD_GroupID int, PROD_GroupName nvarchar(200))<br />
DECLARE @Product TABLE(PROD_ProductID int, PROD_ProductName nvarchar(200), PROD_GroupID int)<br />
DECLARE @InternalNodePath nvarchar(2000)<br />
<br />
-- Get product group data<br />
INSERT @Group SELECT PROD_GroupID, PROD_GroupName FROM fnGroupListByParentID(@CurrentGroupID, @LanguageCode)<br />
<br />
-- DECLARE CURSOR --<br />
DECLARE myCursor CURSOR<br />
FOR <br />
	SELECT PROD_GroupID FROM @Group<br />
OPEN myCursor<br />
<br />
FETCH NEXT FROM myCursor INTO @GroupID<br />
WHILE (@@FETCH_STATUS <> -1 AND @@FETCH_STATUS <> -2) BEGIN<br />
	-- Get Product data<br />
	INSERT @Product SELECT PROD_ProductID, PROD_ProductName, PROD_GroupID FROM fnProductListByGroupID(@GroupID,@LanguageCode)<br />
	<br />
	-- Set path	<br />
	SET @InternalNodePath = @NodePath + '/' + LTRIM((str(@GroupID)))<br />
<br />
	-- Build ProductStructure Table<br />
	INSERT @ProductStruct <br />
		SELECT @InternalNodePath, T1.PROD_GroupID, T1.PROD_GroupName, T2.PROD_ProductID, T2.PROD_ProductName<br />
		FROM @Group AS T1 LEFT JOIN @Product AS T2<br />
		ON T1.PROD_GroupID = T2.PROD_GroupID <br />
	<br />
	-- Build ProductStructure table with Recursive data.<br />
	INSERT @ProductStruct SELECT * FROM dbo.fnGroupListByParentRecusive(@GroupID, @InternalNodePath, @LanguageCode)<br />
	<br />
	-- Get Next cursor<br />
	FETCH NEXT FROM myCursor INTO @GroupID<br />
END<br />
CLOSE myCursor<br />
DEALLOCATE myCursor<br />
RETURN<br />
END</small>


My problem is that this works somewhat.
I apparently get the results from the tables repeated a number of times. The way I figured this out was by view in the NodePath.
This path is the nodes to get the to current, inclusive the current nodes group id.
For instance - the 7 root elements would get the path 0/groupid, and these elements children would get 0/groupid/groupid2 etc.
However - say my Groups have IDs 1, 2, 3 I get the result
<br />
Path          GroupID<br />
0/1           1<br />
0/1           2<br />
0/1           3<br />
0/2           1<br />
0/2           2<br />
0/2           3<br />
0/3           1<br />
0/3           2<br />
0/3           3<br />

Which is not what I wanted.
I wanted
<br />
0/1           1<br />
0/2           2<br />
0/3           3<br />


Now my question and problem is that I want to get from the first list to the second list.
I have an idea that the problem would be that some variables in my function are of global scope as opposed to what I'd expect from for instance a programming language, so each time I run

-snip-<br />
INSERT @ProductStruct <br />
		SELECT @InternalNodePath, T1.PROD_GroupID, T1.PROD_GroupName, T2.PROD_ProductID, T2.PROD_ProductName<br />
		FROM @Group AS T1 LEFT JOIN @Product AS T2<br />
		ON T1.PROD_GroupID = T2.PROD_GroupID <br />
	<br />
	-- Build ProductStructure table with Recursive data.<br />
	INSERT @ProductStruct SELECT * FROM dbo.fnGroupListByParentRecusive(@GroupID, @InternalNodePath, @LanguageCode)<br />
-snip-<br />


then it updates the same table, instead of using a function scoped table which it returns through my recursive call.

However - until today I've never even used userdefined functions, so my knowlegde of this area is very limited (I'm surprised I've gotten this far).

Can anybody help point me in the proper direction and possible tell me if my fear of scope is correct?

With regards and hopes.
Alsvha

---------------------------
127.0.0.1 - Sweet 127.0.0.1
Generalopening documents without saving to disk Pin
carabrenna8-Jul-05 0:26
carabrenna8-Jul-05 0:26 
GeneralRe: opening documents without saving to disk Pin
Colin Angus Mackay8-Jul-05 3:13
Colin Angus Mackay8-Jul-05 3:13 
GeneralRe: opening documents without saving to disk Pin
Anonymous8-Jul-05 10:29
Anonymous8-Jul-05 10:29 
GeneralSQL vs. Access and ODBC Pin
-- NA --7-Jul-05 21:17
-- NA --7-Jul-05 21:17 
GeneralRe: SQL vs. Access and ODBC Pin
RajithCAlwis7-Jul-05 21:45
RajithCAlwis7-Jul-05 21:45 
GeneralRe: SQL vs. Access and ODBC Pin
-- NA --8-Jul-05 22:25
-- NA --8-Jul-05 22: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.