Not sure what the function is doing

Hi,
i have below function i have few question
1)how can i convert this function to sql query and see what it is doing.

Thanks


GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


ALTER   FUNCTION [FAITCAdmin].[eclmentMultipleToOne](@epeObjectId bigint)
RETURNS varchar(200)
AS
BEGIN
      DECLARE @eclList as varchar(20)
      DECLARE @eclResult as varchar(200)
--    DECLARE @epeObjectId as bigint


      DECLARE AllECLs  cursor LOCAL SCROLL FOR
      select ea.ECL
      from ment a, ECLment ea
      where a.EPEObjectID = @epeObjectId
      and a.mentID = ea.mentID
      and a.mentID = (SELECT max(a.mentID) --distinct 'N-' + cast(ea.ControlLevelECLID as varchar(1))
                        from ment a, ECLment ea
                        where a.EPEObjectID = @epeObjectId
                        and a.mentID = ea.mentID)
      FOR  READ ONLY
     
      OPEN AllECLs
      FETCH AllECLs INTO  @eclList  

      WHILE ( @@fetch_status  = 0 )
      BEGIN
            if(len(rtrim(@eclList)) <= 0)
            begin
                  --set @eclList = 'N/A'
                  set @eclList = '-'
            end
     
            set @eclResult = @eclResult + rtrim(@eclList) + ', '
     
            FETCH AllECLs INTO  @eclList  
      END
      IF(len(@eclResult) > 0)
      BEGIN
            set @eclResult = substring(@eclResult, 1, len(rtrim(@eclResult)) -1)
      END
      ELSE
      BEGIN
            set @eclResult = 'N/A'
      END

      CLOSE AllECLs
      DEALLOCATE AllECLs
      return @eclResult
END
sam2929Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Remove the function envelope. Declare the parameters. Assign them meaningful test values. Replace the RETURN with a SELECT. E.g.

DECLARE @epeObjectId BIGINT; 
SET @epeObjectId = 123213; -- your test parameter.

DECLARE @eclList AS VARCHAR(20);
DECLARE @eclResult AS VARCHAR(200);

DECLARE AllECLs CURSOR LOCAL SCROLL
FOR
    SELECT  ea.ECL
    FROM    ment a ,
            ECLment ea
    WHERE   a.EPEObjectID = @epeObjectId
            AND a.mentID = ea.mentID
            AND a.mentID = ( SELECT MAX(a.mentID)
                             FROM   ment a ,
                                    ECLment ea
                             WHERE  a.EPEObjectID = @epeObjectId
                                    AND a.mentID = ea.mentID
                           ) FOR READ ONLY;
       
OPEN AllECLs;
FETCH AllECLs INTO @eclList;  

WHILE ( @@fetch_status = 0 )
    BEGIN 
        IF ( LEN(RTRIM(@eclList)) <= 0 )
            BEGIN
                SET @eclList = '-';
            END;
       
        SET @eclResult = @eclResult + RTRIM(@eclList) + ', ';
       
        FETCH AllECLs INTO @eclList;   
    END;
IF ( LEN(@eclResult) > 0 )
    BEGIN
        SET @eclResult = SUBSTRING(@eclResult, 1, LEN(RTRIM(@eclResult)) - 1);
    END;
ELSE
    BEGIN
        SET @eclResult = 'N/A';
    END;

CLOSE AllECLs;
DEALLOCATE AllECLs;
        
SELECT  @eclResult;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Devinder Singh VirdiLead Oracle DBA TeamCommented:
This function opens cursor and perform grouping based on input. This can be written using SQL command as well, which will be much faster than Function.
Vitor MontalvãoMSSQL Senior EngineerCommented:
sam2929, do you still need help with this question?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.