简体   繁体   English

循环遍历sql(SQL或Asp.net C#)的最佳方法是什么?

[英]What is the best way to loop through sql (SQL or Asp.net C#)?

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' 因此,例如,如果我登录并单击SQL中的提交按钮,它会说我向'John Doe'报告

I then need to grab who 'John Doe' reports to and add that to the list. 然后,我需要抓住“John Doe”报告的人并将其添加到列表中。 I need to keep climbing the list until we reach the top of the company (the GID will be blank or null). 我需要继续攀登列表,直到我们到达公司的顶部(GID将为空或空)。

Using me as an example, I report to 'John Doe' who reports to 'Tom Doe' . 以我为例,我向'John Doe'报告,他向'Tom Doe'汇报。 Tom reports to no-one his usrReportsTo field is like this '00000000-0000-0000-0000-000000000000' . Tom向没有人报告他的usrReportsTo字段就像这个'00000000-0000-0000-0000-000000000000'

If the usrReportsTo field is "" or NULL or '00000000-0000-0000-0000-000000000000' the loop should terminate. 如果usrReportsTo字段为""NULL'00000000-0000-0000-0000-000000000000'则循环应终止。

Here is the sql I used. 这是我用过的sql。

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#? 执行此循环的最干净/最好/最有效的方法是什么?在SQL或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 编辑#3

Working copy of SQL just doesn't return 100% true data. SQL的工作副本不会返回100%真实数据。

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' 使用cte AS(选择usrGID,RTRIM(usrFirstName)+''+ RTRIM(usrLastName)作为名称,usrEmailAddress,usrReportsTo来自dbo.CVT_Users_usr union all选择u.usrGID,RTRIM(u.usrFirstName)+''+ RTRIM(u。 usrLastName),cte.usrEmailAddress,cte.usrReportsTo来自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 usrGID名称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? usrGID和名称是否应该与usrEmailAddress和usrReportsTo相同? I tried chaanging the sql to be cte.USRGID and cte.Name but it gave the max recursion error. 我尝试将sql变为cte.USRGID和cte.Name,但它给出了最大递归错误。

Any ideas? 有任何想法吗?

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. 使用common table expression您可以在一个SQL语句中生成完整的结果集(通过递归连接),从而避免任何循环。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM