我有一个 T-SQL 函数来返回统计属性的所有关联列,我正在传递 sys.stats 系统表的 stats_id 和 object_id。我需要用“,”分隔的所有列,但我的函数只返回第一列。请帮助我在 Sql server 2005 上工作
create FUNCTION fGetstatscols (
@objectid INT,
@stats_id INT
)
RETURNS NVARCHAR(200)
AS
BEGIN
DECLARE @V_IncludedCol NVARCHAR(200)
DECLARE collist CURSOR
( select (name) from sys.columns where Object_id =@objectid
and column_id in (select column_id from sys.stats_columns where object_id=@objectid
and stats_id=@stats_id))
OPEN collist
FETCH NEXT FROM collist INTO @V_IncludedCol
WHILE @@FETCH_STATUS <> 0
begin
Select @V_IncludedCol = COALESCE(@V_IncludedCol + ',', '')
END
close collist
deallocate collist
RETURN @V_IncludedCol
END