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

时间:2015-04-28 15:05:54

标签: sql sql-server-2008 user-defined-functions udf

这是我的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

我希望第二行中的列注释值相同...但我得到之前的值

我需要

注释

第二次拒绝评论

第二次拒绝评论

我得到:(

第二次拒绝评论

关于第二项要求的另外一条评论

1 个答案:

答案 0 :(得分:0)

你的SQL中有这一行:

comments = dbo.udfGetCommentListRajendra(r.firmcompliancerequirementid),

理所当然,如果第一条记录中的r.firmcompliancerequirementid与第二条记录中的r.firmcompliancerequirementid不同,那么您将获得不同的返回值。

如果它们相同,并且您从dbo.udfGetCommentListRajendra获得了不一致的结果,则表RequirementCommentID中的requirementcomment可能有多个值,而您不是获得相同的记录每次突然出现在顶部。

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

因此,我会问你想要这种格式的数据的目的。这是出现在用于查看用户的网页,报告或类似内容上吗?

如果是这样,请记住,您始终可以更改屏幕或报告中的输出内容。按原样返回数据可能有意义,但修改报表以便在向下翻页时重复第一条注释行。