[英]Convert PL SQL function to T-SQL with cursor loop and fetch
The code for the function is as follows:该函数的代码如下:
CREATE OR REPLACE FUNCTION FTTH_GETBUSZONECODEMULTI(
p_house_nbr IN VARCHAR2,
p_as_of_date IN DATE DEFAULT SYSDATE)
RETURN VARCHAR2
AS
CURSOR l_get_cur
IS
SELECT
LTRIM(RTRIM(BZO.B2FEDC)) BusinessZoneCode
FROM
[not wasting your time with business logic]
l_return VARCHAR2(32767);
BEGIN
--
FOR l_get_rec IN l_get_cur LOOP
l_return := l_return || '|' || l_get_rec.BusinessZoneCode;
END LOOP;
--
CASE
WHEN l_return IS NULL THEN RETURN NULL;
ELSE RETURN l_return || '|';
END CASE;
--
END FTTH_GETBUSZONECODEMULTI;
My attempt at translating it is below:我的翻译尝试如下:
CREATE FUNCTION ftth_GETBUSZONECODEMULTI(
@p_house_nbr VARCHAR(4000),
@p_as_of_date DATETIME)
RETURNS VARCHAR(4000)
AS
BEGIN
SET @p_as_of_date = GETDATE()
DECLARE l_get_cur CURSOR LOCAL
FOR
SELECT
LTRIM(RTRIM(BZO.B2FEDC)) BusinessZoneCode
FROM
[not wasting your time with business logic]
DECLARE @l_return VARCHAR(MAX);
--
SET @l_return = isnull(@l_return, '') + '|' + ISNULL((FETCH BusinessZoneCode from l_get_cur), '');
--
if @l_return IS NULL begin RETURN NULL END
if @l_return is not null BEGIN RETURN isnull(@l_return, '') + '|' END;
END
The problem is with, I think, how I'm trying to FETCH the value - even if I put a "NEXT" in there it doesn't work right.我认为问题在于我如何尝试获取值 - 即使我在其中放置了“NEXT”,它也无法正常工作。 I've tried like 6 different ways to arrange the FETCH and none of them work.
我已经尝试了 6 种不同的方式来安排 FETCH,但都没有奏效。
here is some changes in cursor usage, I might be wrong where you cursor loop end , so you might need to adjust it:这是游标使用的一些变化,我可能在游标循环结束的地方错了,所以你可能需要调整它:
CREATE FUNCTION ftth_GETBUSZONECODEMULTI(
@p_house_nbr VARCHAR(4000),
@p_as_of_date DATETIME)
RETURNS VARCHAR(4000)
AS
BEGIN
declare @BusinessZoneCode varchar(500)
SET @p_as_of_date = GETDATE()
DECLARE l_get_cur CURSOR LOCAL
FOR
SELECT
LTRIM(RTRIM(BZO.B2FEDC)) BusinessZoneCode
FROM
[not wasting your time with business logic]
OPEN l_get_cur
fetch next from l_get_cur
into @BusinessZoneCode
WHILE @@FETCH_STATUS = 0
begin
DECLARE @l_return VARCHAR(MAX);
--
SET @l_return = isnull(@l_return, '') + '|' + ISNULL((@BusinessZoneCode), '');
--
if @l_return IS NULL begin RETURN NULL END
if @l_return is not null BEGIN RETURN isnull(@l_return, '') + '|' END
fetch next from l_get_cur
into @BusinessZoneCode
end
close l_get_cur
deallocate l_get_cur
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.