我郁闷,一句就搞定的事怎么....create database test
go
use test
go
create table t1
(
tid int,
tvalue nvarchar(5)
)
go
insert into t1 values(1,'aaa')
insert into t1 values(1,'bbb')
insert into t1 values(1,'ccc')
select * from t1
go
----------------
declare @str varchar(20)
set @str=''
select @str=@str+ tvalue from t1 where tid=1
print @str
[Quote=引用 25 楼 zhou968 的回复:]
SQL code
Create table Tab([Col1] int,[Col2] nvarchar(1))
Insert Tab
select 1,N'a' union all
select 1,N'b' union all
select 1,N'c' union all
select 2,N'd' union all
select 2,N'e' union all
select 3,N'f'
Go
create function F_Str(@Col1 int)
returns nvarchar(100)
as
begin
declare
@S nvarchar(100)
select @S=isnull(@S+',','')+Col2 from Tab where Col1=@Col1
re…
[/Quote]
-- 示例数据
DECLARE @t TABLE(id int, value varchar(10))
INSERT @t SELECT 1, 'aaa'
UNION ALL SELECT 1, 'bbb'
UNION ALL SELECT 1, 'ccc'
-- 查询处理
SELECT *
FROM(
SELECT DISTINCT
id
FROM @t
)A
OUTER APPLY(
SELECT [values]= STUFF(REPLACE(REPLACE(
(
SELECT value FROM @t N
WHERE id = A.id
FOR XML AUTO
), '<N value="', ''), '"/>', ''), 1, 1, '')
)N