CROSS APPLY 不适用于 SQL SERVER 2000?

发布于 2024-10-11 12:15:50 字数 1601 浏览 2 评论 0原文

如何在 SQL Server 2000 中使用与 CROSS APPLY 等效的功能?

我有一个函数,它返回传递的 id 的顶级父级。

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

在查询中使用如下,

      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

它在 2000 年不起作用。我明天发布。请帮忙!!

我将不胜感激

谢谢

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 grateful

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知在何时 2024-10-18 12:15:50

由于您使用的函数始终仅返回一个结果,因此您不需要交叉应用。应该可以将表值函数更改为标量值函数。

SQL 语句

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

标量值函数

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文