处理前的查询结果如上图:
通过借助SQL变量的定义
DECLARE @Scope varchar(1000) DECLARE @Num int SET @Scope='' SET @Num = 1 SELECT @Scope=@Scope+convert(varchar(8),@Num)+'、'+DescScope+ DescOper+';', @Num = @Num+1 From fuel_contractQualityNew Where ContractID='012009000100' SELECT ContractID,@Scope FROM fuel_contractQualityNew Where ContractID='012009000100' GROUP BY ContractID
实现了一下效果:
//创建拼接描述SQL的函数 CREATE FUNCTION dbo.f_str(@contractID varchar(50)) RETURNS VARCHAR(8000) AS BEGINDECLARE @Scope varchar(1000)DECLARE @Num int SET @Scope=''SET @Num = 1 SELECT @Scope=@Scope+convert(varchar(8),@Num)+''+DescScope+ DescOper+';', @Num = @Num+1 From fuel_contractQualityNew where ContractID= @contractIDRETURN @Scope END GO