运行存储过程时出现以下错误。该存储过程调用一个函数,该函数使用返回表值函数的函数来声明表的游标。请注意,我没有包含代码的声明部分
declare cust_trn cursor local for SELECT * FROM [dbo].[Q03_Create_Customer_Trn](@COUNTERPARTY)
IF CURSOR_STATUS('local','cust_trn') <>-1
BEGIN
OPEN cust_trn
END
FETCH NEXT FROM cust_trn INTO @trn,@cust_name;
WHILE @@FETCH_STATUS=0
BEGIN
IF (@LIST1 IS NULL) BEGIN
SET @LIST1= @cust_name;
END
ELSE BEGIN
SET @LIST1=ISNULL(@LIST1, '') + ';' + ISNULL(@cust_name, '');
END
FETCH NEXT FROM cust_trn INTO @trn,@cust_name; END END ELSE BEGIN SET @LIST1 = 'N/A'; END CLOSE cust_trn ; DEALLOCATE cust_trn; RETURN ISNULL(@LIST1,'N/A'); END; ISNULL(@LIST1,'N/A'); END;
错误是名称为'cursor_name'的游标不存在,该函数开始名为 dbo.Q03_get_list_of_counterparties(COUNTERPARTY,'Y')CParties 。存储过程。
答案 0 :(得分:1)
您可以按照以下方式将整个光标逻辑重写为某种形式。
declare @List varchar(500) = '' --use whatever size is relevant.
select @List = @List + cust_name + ';'
from [dbo].[Q03_Create_Customer_Trn](@COUNTERPARTY)
select @List = left(@List, len(@List) - 1) --removes the last comma
另一个选择是使用STUFF和FOR XML。互联网上有成百上千的例子。