September 24, 2012 at 5:10 pm
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
September 26, 2012 at 3:59 am
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?
September 26, 2012 at 4:28 am
September 26, 2012 at 5:05 am
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
September 26, 2012 at 5:33 am
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.
September 26, 2012 at 6:02 am
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
September 26, 2012 at 8:19 am
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
September 26, 2012 at 10:37 am
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 here1) 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
September 26, 2012 at 11:59 am
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
September 26, 2012 at 12:09 pm
roh_ranjan (9/26/2012)
Hello SeanThank 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
Sure I can help.
If you want some real help with your code please take a look at the first link in my signature about best practices.
You say you agree but you still haven't posted any ddl or sample data.
_______________________________________________________________
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
September 26, 2012 at 12:26 pm
Hello Sean,
Here is the DDL statment and sample data
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
--select * from @sys_user
DECLARE @userid BIGINT--=800000000
Set @userid=800000000
;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
Hope it will work for you.Please let me know if you need any more information.
Thanks
Rohit
September 26, 2012 at 12:35 pm
Well I know have another copy of the ddl and sample that somebody else wrote but I still don't know what you want out of this. I think you are asking about this:
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.
This all seems to me like permissions that are NOT part of the database at all.
1. You mention Super Admin, what is the definition of Super Admin?
2. I can't even begin to understand what you mean here.
3. I can't even begin to understand what you mean here.
4. You mention Admin, what is the definition of Admin?
How are you planning on implementing this in the database?
_______________________________________________________________
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
September 26, 2012 at 1:01 pm
Hello Sean
What i mean to say that user have maximum right to perform addition/deletion/substitution.Assuming I/you have all access and now request came to add/delete/substitute user in current hierarchy showing.
a) Deletion/substitution process
Deleting a single node in the middle of the tree then fill up the existing user into the vacancies as created.
b) Addition/updating Process
Add the user in middle/anywhere of node of the tree.Updates to the nodes are performed by searching for the key of each node.if user exist then do not insert else insert the user.We have to consider the level when we are adding /updating user(using userid).
Let me know if you need any more information
Thanks again for your help
R
September 26, 2012 at 1:13 pm
roh_ranjan (9/26/2012)
Hello SeanWhat i mean to say that user have maximum right to perform addition/deletion/substitution.Assuming I/you have all access and now request came to add/delete/substitute user in current hierarchy showing.
a) Deletion/substitution process
Deleting a single node in the middle of the tree then fill up the existing user into the vacancies as created.
I don't know what you mean. Deleting a row is just a delete statement. What do you mean by filling up the vacancy? Are you saying if a node is removed that the child of that row moves up a level?
b) Addition/updating Process
Add the user in middle/anywhere of node of the tree.Updates to the nodes are performed by searching for the key of each node.if user exist then do not insert else insert the user.We have to consider the level when we are adding /updating user(using userid).
An insert statement? I don't get what you mean at by consider the level when adding.
Let me know if you need any more information
Thanks again for your help
R
I know you are pretty new around here but your question is just entirely to vague. You have an idea of what you want to do but I don't have a clue. I think you read back through your posts from the perspective of somebody outside your company you would see that you don't have anywhere near enough information about what you are trying to do. I really am willing to help but you have to provide enough information. Take a look at this link. http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
_______________________________________________________________
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
September 27, 2012 at 2:58 am
sorry for not replying sooner.
The Insert and Delete from the table are fairly straight forward, this issue is that you need to be able to move people in sub trees.
The difficulty are the rules around permissions, as you need to know how to identifty Admins and Non-admins. As the permissions for users of a specific Level, are we talking about the level in the Hierarchy, ie can a user on level 1 of the hierarchy add anyone to anywhere in the second tier of the hierarchy or can he only add/remove people from his subtree.
I suspect that an individual can only add/delete subordinates from his subtree, but this needs to be confirmed.
You also need to define what happens to the subordinates of anyone that gets removed from the subtree, who do they attach to if there is no replacement.
All this needs to be understood by us, as sean has stated posing the DDL of the table sys_user that you have in front of you with sample data will help us understand the data and alow us to help you a lot quicker.
Dont rely on the data I created it was very nody based on your initial post and was there to show a method of creating what your original DDL did without the cursor and nested SP's.
_________________________________________________________________________
SSC Guide to Posting and Best Practices
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply