SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need Help on User Tree Hierarchy with Addition/Deletion Option


Need Help on User Tree Hierarchy with Addition/Deletion Option

Author
Message
roh_ranjan
roh_ranjan
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 132
Hello All,

I have situation where if user pass the userid then their respective hierarchy display.Here is SP

CREATE PROCEDURE TraverseUsersRecursive
@UserId INT
AS

/* to change action on each vertex, change these lines */
DECLARE @Name VARCHAR(100)
SELECT @Name=(SELECT (FRST_NM + '' + LST_NM) as name
FROM sys_usr WHERE usrid=@UserId)
PRINT SPACE(@@NESTLEVEL*2)+STR(@UserId)+'|| '+@Name
DECLARE submenu CURSOR LOCAL FOR
SELECT usrid FROM sys_usr WHERE APROV_ID=@UserId
OPEN submenu
FETCH NEXT FROM submenu INTO @UserId
WHILE @@FETCH_STATUS=0 BEGIN
EXEC TraverseUsersRecursive @UserId
FETCH NEXT FROM submenu INTO @UserId
END
CLOSE submenu
DEALLOCATE submenu

Result will be(please see the attachment)
800000000|| Dave Smith
810000000|| Michael Chak
811000000|| David Boon
811100000|| Glen Macgrath
811110000|| Alex Boon
811111000|| Jim Foley
811111100|| Sean Rody
811111110|| Lucian
811111111|| Ferrer
821000000|| Stephan
822000000|| Robin
811111110|| Miranda
811111112|| Sohail
823000000|| Imran
824000000|| Amir

Now I have situation where I need to add/delete new user(deletion happen only by Admin),each user can add other users and any dummy user will replace any user.
Can any body help me to design SP.
Appreciate your help.
Thanks in Advance
R
laurie-789651
laurie-789651
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1522 Visits: 1272
The usual solution for this is a recursive CTE.

Look at
http://www.sqlservercentral.com/articles/Development/recursivequeriesinsqlserver2005/1760/
for example.

If you post some proper test data it will be easier for people to help!

Also, do you want help designing an sp to display the data, or to make the changes?
vinu512
vinu512
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3813 Visits: 1626
Laurie is right. Please post DDL, Sample Data and the Expected Result and we will give you a sample solution using a Recursive CTE.

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
Jason-299789
Jason-299789
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5187 Visits: 3232
This should do the trick, but with out example data it might be off the mark, luckily I was at a loose end for an hour or to and pus some data together based on the sample.


DECLARE @sys_user TABLE
(UserId bigint
,FRST_NM varchar(100)
,LST_NM varchar(100)
,APROV_ID bigint)

Insert into @sys_user
Select 800000000, 'Dave', 'Smith', NULL
UNION Select 810000000, 'Michael', 'Chak',800000000
UNION Select 811000000, 'David', 'Boon',810000000
UNION Select 811100000, 'Glen','Macgrath',811000000
UNION Select 811110000, 'Alex', 'Boon',811100000
UNION Select 811111000, 'Jim', 'Foley',811111000
UNION Select 821000000,'Stephan', '',800000000

DECLARE @UserId BIGINT=800000000

select * from @sys_user

;With submenu (UserId,Name,APROV_ID,lvl)
AS
(
Select UserId, FRST_NM + ' '+LST_NM as Name,APROV_ID, 0 lvl
From @sys_user
Where UserId=@UserId
Union ALL Select
su.UserId
,su.FRST_NM + ' '+su.LST_NM
,su.APROV_ID
,sm.lvl+1
From @sys_user su
Join submenu sm ON su.APROV_ID=sm.UserId
)
Select SPACE(lvl*2)+STR(UserID) + '|| '+Name,lvl
From submenu
Order by UserId



PS: I see you're a bit of a cricket fan, hope you're enjoying the T20.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
roh_ranjan
roh_ranjan
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 132
Thank you all for response. Now my next question is how to handle new user addition / substitution if any user is on leave. My thought is replace temp user with absent user.for example: if a = 1,b = 2 then using temp variable I have to swap a= 2 ,b = 1.please help me in this.
Jason-299789
Jason-299789
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5187 Visits: 3232
I think to help with the substitution we're going to need sample data, and the rules.

I take it sys_user has a column or lookup to another table to show when a person is on holiday and who is covering for them, but without the structures and sample data we will struggle to help any further.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
roh_ranjan
roh_ranjan
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 132
Hello All, I have already provided the Cursor(sort of DDL) which are creating the tree view. Also i tried to provide the tree view result of cursor.Thank you re write in recursive way to handle the tree view.Answer to Jason question, User information is define in single table,which include user name, user id ,approver id ,Role .Rule which I need to define here

1) Super Admin can add any user at any node level.First it will scan whether userid is exist or not then new add new user.
2) In case of absent of user, another user(same level or any different level user) will perform his/her role activity
3) Once user will come back then user will insert in same role
4) Admin can delete any user (in case of user leave the org) and corresponding child node will assign to another user.

These are above rule need to implement.Please help me in this.
Thanks
Rohit
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65274 Visits: 17980
roh_ranjan (9/26/2012)
Hello All, I have already provided the Cursor(sort of DDL) which are creating the tree view. Also i tried to provide the tree view result of cursor.Thank you re write in recursive way to handle the tree view.Answer to Jason question, User information is define in single table,which include user name, user id ,approver id ,Role .Rule which I need to define here

1) Super Admin can add any user at any node level.First it will scan whether userid is exist or not then new add new user.
2) In case of absent of user, another user(same level or any different level user) will perform his/her role activity
3) Once user will come back then user will insert in same role
4) Admin can delete any user (in case of user leave the org) and corresponding child node will assign to another user.

These are above rule need to implement.Please help me in this.
Thanks
Rohit


A cursor is nothing like DDL. A cursor is a poor performing example of DML. DDL is create table statements. The problem is that we can't see from here what you see. We don't know the project and we don't have any tables to run queries against.

If you want some real help with your code please take a look at the first link in my signature about best practices.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
roh_ranjan
roh_ranjan
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 132
Hello Sean

Thank you for response.I completely agree what you are saying.If you see the above Jason response where he created temp table and show the hierarchy.In same table I need to perform addition/deletion/substitution of the user.Can you help me in this.
Thanks
R
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search