CROSS APPLY 不适用于 SQL SERVER 2000?
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您使用的函数始终仅返回一个结果,因此您不需要交叉应用。应该可以将表值函数更改为标量值函数。
SQL 语句
标量值函数
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
Scalar Valued function