sql 用户定义的函数来检索相同的值

sql user defined function to retrieve same value

这是我的asp.net方法

 public static DataSet ProgressReport(int FirmID)
    {
        if (HttpContext.Current.Session["CNYearID"] != null && HttpContext.Current.Session["CNYearID"].ToString() != "")
        {
            firmcomplianceyear = Convert.ToInt32(HttpContext.Current.Session["CNYearID"]);
        }
        else if (HttpContext.Current.Session["CurrentFCY"] != null && HttpContext.Current.Session["CurrentFCY"].ToString() != "")
        {
            firmcomplianceyear = Convert.ToInt32(HttpContext.Current.Session["CurrentFCY"]);
        }



        if (HttpContext.Current.Session["NewCompYID"] != null && HttpContext.Current.Session["NewCompYID"].ToString() != "")
        {
            compliance_year = Convert.ToInt32(HttpContext.Current.Session["NewCompYID"]);
        }
        else if (HttpContext.Current.Session["CompYearID"] != null && HttpContext.Current.Session["CompYearID"].ToString() != "")
        {
            compliance_year = Convert.ToInt32(HttpContext.Current.Session["CompYearID"]);

        }
        String str = String.Format(@"select 
        F.firmname, 
        fr.firmusername as FirmUserName,
        S.sectionnumber, 
        d.deptname as Department, 
        CONVERT(VARCHAR(10),R.InsertDate, 101) AS Date,
        st.statusname,
        Convert(decimal(10,2),(st.StatusPercentComplete/cast (100 as float))) as Percentage,                
        case when R.PolicyConfirmation = '0' then 'NO'
        when R.PolicyConfirmation = '1' then 'YES'
        end as PolicyConfirmation,
        case when R.GeneralConfirmation='0' then 'NO'
        when R.GeneralConfirmation='1' then 'YES' end  as GeneralConfirmation,
        comments = dbo.udfGetCommentListRajendra(r.firmcompliancerequirementid),
        documents = dbo.udfGetDocumentList( F.firmid,S.sectionid),

---------我在这里使用下面的 udf 函数,我被卡住了----

        Location=dbo.udfGetLocationList_1( F.firmid,S.sectionid,fr.firmuserid)
        from requirementcertification r
        join firm  f on F.firmid=R.firmid
        join Section s on S.sectionid= R.sectionid
        join FirmComplianceRequirement fcr on Fcr.FirmComplianceRequirementID = R.FirmComplianceRequirementID
        Join FirmComplianceYear FCY on FCY.FirmComplianceYearID=FCR.firmcomplianceyearid
        join Department d on d.deptid=fcr.assignedto
        join [Status] St on st.statusID = R.statusid
        join firmuser fr on fr.firmuserid = r.userid

         where r.userid in(select firmuserid from firmfirmusermapping where firmid='{0}') AND (D.FirmID=0 OR D.FirmID= '{0}') AND D.IsActive=1 AND D.IsDelete=0 And  FCY.ComplianceYearID='{1}' 
        order by R.insertdate desc", FirmID, compliance_year);
        DataSet ds = DAL.SelectRecords(str.ToString());
        return ds;
    }

.

USE [Mydata]
GO


Create  function [dbo].[udfGetCommentList]
(
@firmcompliancerequirementid int
)
RETURNS varchar(max)
as
BEGIN

DECLARE @listStr VARCHAR(MAX)
--SELECT @listStr = COALESCE(@listStr+',' ,'') + requirementcomment
--FROM requirementcomment  
--where firmcompliancerequirementid = @firmcompliancerequirementid 

Select distinct @listStr= RequirementComment from requirementcomment 
where firmcompliancerequirementid=@firmcompliancerequirementid 
order by RequirementCommentID desc
return @listStr
END


GO

这个输出:

Name        Section#    Department   Date        Status                 %   Comments
vishalVVVV  23.503     Compliance   04/28/2015  Pending Documentation   25% Rejection comment for the second time
vishalVVVV  23.503  Finance 04/28/2015  Pending Documentation   25%           one more comment on 2nd requirement

我也希望第二行中的列注释具有相同的值...但我得到的是以前的值

我需要

评论


第二次拒绝评论

第二次拒绝评论

我越来越:(


第二次拒绝评论

关于第二个要求的更多评论

您的 SQL 中有此行​​:

comments = dbo.udfGetCommentListRajendra(r.firmcompliancerequirementid),

按理说,如果第一条记录的 r.firmcompliancerequirementid 与第二条记录的 r.firmcompliancerequirementid 不同,您将获得不同的 return 值。

如果它们相同,而您从 dbo.udfGetCommentListRajendra 得到的结果不一致,则可能是 table requirementcomment 中的 RequirementCommentID 有多个值] 而且您不会每次都获得相同的记录。

但是,如果它们不同,但您希望结果好像它们是相同的...那么在单个查询中实现会很复杂。

因此,我想问一下您想要这种格式的数据的目的是什么。这是要出现在网页、报告或类似的东西上,供用户查看吗?

如果是这样,请记住您可以随时更改屏幕或报告中的输出内容。 return 您现在所做的数据可能有意义,但修改您的报告,以便在您向下浏览页面时重复第一条评论行。