开发者

CROSS APPLY does not work with SQL SERVER 2000?

开发者 https://www.devze.com 2023-02-03 14:54 出处:网络
How can I use something equivalent of CROSS APPLY in SQL Server 2000 ? I have a function which returns top level parent of id passed.

How can I use something equivalent of CROSS APPLY in SQL Server 2000 ?

I have a function which returns top level parent of id passed.

ALTER Function [dbo].[fn_GetTopParentRiskCategory]
(
 @RctId int
)
RETURNS @TEMP_TABLE TABLE
(
 rctId int,
 topParentRiskCat Varchar(100)
)
AS 
BEGIN
  DECLARE @PARENTID INT
  DECLARE @GRANDPARENTID INT
  DECLARE @CODE VARCHAR(100)
  DECLARE @DESC VARCHAR(100)
  DECLARE @PARENTCODE VARCHAR(100)


  SELECT @PARENTID= rctParentID from RiskCategory where rctid=@RctId

  SELECT @GRANDPARENTID= rctParentId from RiskCategory where rctid= @PARENTID
  IF @GRANDPARENTID IS NULL
  BEGIN
   SELECT @CODE='ALL'
   INSERT INTO @TEMP_TABLE SELECT @Rctid,@CODE
  END
  ELSE
  BEGIN
   SELECT @CODE=''
   SELECT @DESC=rctDescription from RiskCategory where rctid=@PARENTID
   WHILE(@PARENTID IS NOT NULL)
   BEGIN
     SELECT @PARENTCODE=rctCode from RiskCategory WHERE rctid=@PARENTID
     IF @PARENTCODE='All'
      SET @PARENTCODE=''
     SELECT @CODE=@PARENTCODE +'.'+ @CODE 
     SELECT @PARENTID =rctParentID from Riskcategory where rctid= @PARENTID 
   END
    SELECT @CODE=Substring(@CODE+@DESC,2,len(@CODE+@DESC))
    INSERT INTO @TEMP_TABLE SELECT @RctID,@CODE
  END

RETURN 
END

Used in query as follows

      INSERT INTO @TopRiskCat
   SELECT R.rskid
       ,a.topParentRiskCat AS TopLevelRiskCat
   FROM RISKS R 
   INNER JOIN RiskAnalysis RA
   ON R.rskId = RA.ranRiskId
   INNER JOIN RiskCategory RC
   ON RA.ranRiskAnalId = RC.rctId
   CROSS APPLY fn_GetTopParentRiskCategory(RC.rctid) as a

It does not work in 2000. I have release tomorrow . Please help !!

I'll be grate开发者_JS百科ful

Thanks


As the function you use always only returns one result, you don't need a cross apply. It should be possible to change your table valued function to a scalar valued function.

SQL Statement

INSERT INTO @TopRiskCat
SELECT  R.rskid
        , a.topParentRiskCat AS TopLevelRiskCat
        , fn_GetTopParentRiskCategory(RC.rctID)
FROM    RISKS R 
        INNER JOIN RiskAnalysis RA ON R.rskId = RA.ranRiskId
        INNER JOIN RiskCategory RC ON RA.ranRiskAnalId = RC.rctId

Scalar Valued function

ALTER Function [dbo].[fn_GetTopParentRiskCategory] (@RctId int)
RETURNS Varchar(100) AS 
BEGIN

  DECLARE @topParentRiskCat Varchar(100)
  DECLARE @PARENTID INT
  DECLARE @GRANDPARENTID INT
  DECLARE @CODE VARCHAR(100)
  DECLARE @DESC VARCHAR(100)
  DECLARE @PARENTCODE VARCHAR(100)


  SELECT @PARENTID= rctParentID from RiskCategory where rctid=@RctId
  SELECT @GRANDPARENTID= rctParentId from RiskCategory where rctid= @PARENTID

  IF @GRANDPARENTID IS NULL
  BEGIN
    SELECT @topParentRiskCat = 'ALL'   
  END
  ELSE
  BEGIN
   SELECT @CODE=''
   SELECT @DESC=rctDescription from RiskCategory where rctid=@PARENTID
   WHILE(@PARENTID IS NOT NULL)
   BEGIN
     SELECT @PARENTCODE=rctCode from RiskCategory WHERE rctid=@PARENTID
     IF @PARENTCODE='All'
        SET @PARENTCODE=''
     SELECT @CODE=@PARENTCODE +'.'+ @CODE 
     SELECT @PARENTID = rctParentID from Riskcategory where rctid= @PARENTID 
   END
    SELECT @CODE=Substring(@CODE+@DESC,2,len(@CODE+@DESC))
    SELECT @topParentRiskCat = @CODE
  END

  RETURN @topParentRiskCat
END
0

精彩评论

暂无评论...
验证码 换一张
取 消