SQL SERVER2000将多行查询结果拼接到一行数据及函数的创建

SQL SERVER2000将多行查询结果拼接到一行数据及函数的创建

处理前的查询结果如上图:

通过借助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=''
SELECT ContractID,@Scope FROM fuel_contractQualityNew Where ContractID='' GROUP BY ContractID

实现了一下效果:

SQL SERVER2000将多行查询结果拼接到一行数据及函数的创建

 //创建拼接描述SQL的函数
CREATE FUNCTION dbo.f_str(@contractID varchar(50))
RETURNS VARCHAR(8000)
AS
BEGIN
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= @contractID
RETURN @Scope
END
GO
上一篇:多个Activity相互调用和Intent


下一篇:android host