Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8407639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:25:17+00:00 2026-06-09T23:25:17+00:00

I am creating a list to send an e-mail to. The individual who is

  • 0

I am creating a list to send an e-mail to. The individual who is logged in has a field in the database of who they report to (unless there is an error or they report to no one).

So for instance if I am logged in and clicked the submit button in SQL it would say I report to ‘John Doe’

I then need to grab who ‘John Doe’ reports to and add that to the list. I need to keep climbing the list until we reach the top of the company (the GID will be blank or null).

Using me as an example, I report to 'John Doe' who reports to 'Tom Doe'. Tom reports to no-one his usrReportsTo field is like this '00000000-0000-0000-0000-000000000000'.

If the usrReportsTo field is "" or NULL or '00000000-0000-0000-0000-000000000000' the loop should terminate.

Here is the sql I used.

What is the cleanest/neatest/most effecient way to perform this loop and is it better to do it in SQL or ASP.net C#?

select usrReportsTo from dbo.CVT_Users_usr, usrEmailAddress
WHERE RTRIM(usrFirstName) + ' ' + RTRIM(usrLastName) = 'James Wilson' 
-- Returns 'James Wilson' + email

SELECT RTRIM(usrFirstName) + ' ' + RTRIM(usrLastName) as Name, usrEmailAddress, usrReportsTo from dbo.CVT_Users_usr
WHERE usrGID = '38922F83-4B6E-499E-BF4F-EF0B094F47F7'
-- Returns 'John Doe' + email + reportsTo

SELECT RTRIM(usrFirstName) + ' ' + RTRIM(usrLastName) as Name, usrEmailAddress, usrReportsTo from dbo.CVT_Users_usr
WHERE usrGID = 'FB8F4939-3956-4834-9D89-D72AFB8BF3E0'
-- Returns 'Tom Doe' + email + reportsTo

Edit #3

Working copy of SQL just doesn’t return 100% true data.

with cte
AS
(
select usrGID, RTRIM(usrFirstName) + ‘ ‘ + RTRIM(usrLastName) as Name, usrEmailAddress, usrReportsTo from dbo.CVT_Users_usr
union all
select u.usrGID, RTRIM(u.usrFirstName) + ‘ ‘ + RTRIM(u.usrLastName), cte.usrEmailAddress, cte.usrReportsTo from dbo.CVT_Users_usr u
inner join cte on u.usrReportsTo = cte.usrGID
)
select * from cte
where Name = ‘James Wilson’

— Returns

usrGID Name usrEmailAddress usrReportsTo
E1DAFC11-BE35-4730-9961-68EEF8D85DE4 James Wilson 38922F83-4B6E-499E-BF4F-EF0B094F47F7
E1DAFC11-BE35-4730-9961-68EEF8D85DE4 James Wilson john@1234.com FB8F4939-3956-4834-9D89-D72AFB8BF3E0
E1DAFC11-BE35-4730-9961-68EEF8D85DE4 James Wilson tom@1234.com 00000000-0000-0000-0000-000000000000

Shouldn’t the usrGID and name match the same way usrEmailAddress and usrReportsTo does? I tried chaanging the sql to be cte.USRGID and cte.Name but it gave the max recursion error.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:25:19+00:00Added an answer on June 9, 2026 at 11:25 pm

    Using a common table expression you can generate the complete result set in one SQL statement (via a recursive join), thus avoiding any looping at all.

    A basic example with the key fields

    create table #CVT_Users_usr (usrGid uniqueidentifier, usrEmailAddress varchar(50), usrFirstName varchar(20), usrLastName varchar(20), usrReportsTo uniqueidentifier)
    
    insert #CVT_Users_usr values    
    ('38922F83-4B6E-499E-BF4F-EF0B094F47F7' , 'james@wilson.com','james','wilson', 'E1DAFC11-BE35-4730-9961-68EEF8D85DE4'),
    ('E1DAFC11-BE35-4730-9961-68EEF8D85DE4', 'john@doe.com','john','doe', 'FB8F4939-3956-4834-9D89-D72AFB8BF3E0'),
    ('FB8F4939-3956-4834-9D89-D72AFB8BF3E0', 'tom@doe.com','tom','doe' ,'00000000-0000-0000-0000-000000000000'),
    ('FE6899A5-63BA-42B7-A70E-011711A5FAC6', 'ken@ken.com','ken', 'kenneths', 'FB8F4939-3956-4834-9D89-D72AFB8BF3E0')
    
    declare @id uniqueidentifier
    select @id = usrGid from #CVT_Users_usr where usrFirstName='james' and usrLastName='wilson'
    
    ;with cte as
    (
    select usrGid, usrEmailAddress, usrFirstName, usrLastName, usrReportsTo from #CVT_Users_usr
    union all
    select u.usrGid,  cte.usrEmailAddress, cte.usrFirstName, cte.usrLastName, cte.usrReportsTo 
    from #CVT_Users_usr u
        inner join cte on u.usrReportsTo= cte.usrGid
    )
        select usrFirstName + ' ' + usrLastName, usrEmailAddress  from cte
        where usrGid=@id
    
    drop table #CVT_Users_usr
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a calendar list system, that has tabbed dates on top, with
I'm creating a mailing list application. It will send 40 emails per hour due
I am creating a WebAPI where the frontend will send back a list of
I'm creating a dynamic todo list. The list is downloaded from a local database
I am creating a category list.. 20 different categories and when they click and
I am creating an Outlook Mail Item object and watching for the Send event.
//------------------------------ //creating dynamic array to send std::list<char> list; char in=NULL; while(1) { scanf(%c,&in); if(in=='\n')
I'm creating a list with itertools from a list of ranges, so far I
I'm creating a list of class Task in a way such as this. List<Task>
In my code I am creating a List<> of the following class: public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.