用到函数的题目
Code
表table1:
┍--------------------------┐
| ID | name |
|--------------------------|
| 1 | A |
| 2 | B |
| 3 | AE |
| 4 | CD |
| 5 | BW |
| 6 | CT |
| 7 | EN |
| 8 | AY |
| . | . |
| . | . |
| . | . |
查询结果是:
┍--------------------------┐
| ID | name |
|--------------------------|
| 1 | A,AE,AY··· |
| 2 | B,BW··· |
| 3 | CD,CT··· |
| 4 | EN··· |
| . | . |
| . | . |
| . | . |
└--------------------------┚
Code
--> 生成测试数据: @T
create TABLE t (ID INT,[name] VARCHAR(2))
INSERT INTO T
SELECT 1,'A' UNION ALL
SELECT 2,'B' UNION ALL
SELECT 3,'AE' UNION ALL
SELECT 4,'CD' UNION ALL
SELECT 5,'BW' UNION ALL
SELECT 6,'CT' UNION ALL
SELECT 7,'EN' UNION ALL
SELECT 8,'AY'
create function F_Str(@name nvarchar(100))
returns nvarchar(100)
as
begin
declare @S nvarchar(100)
select @S=isnull(@S+',','')+name from T where name like left(@name,1)+'%'
return @S
end
select distinct (dbo.F_str([name]) ) [name] into #t2 from t
Select identity(int,1,1) as iid,* into #tmp from #t2
select * from #tmp
如果
create TABLE t (ID INT,[name] VARCHAR(2))
INSERT INTO T
SELECT 1,'A' UNION ALL
SELECT 1,'B' UNION ALL
SELECT 1,'AE' UNION ALL
SELECT 1,'CD' UNION ALL
SELECT 2,'BW' UNION ALL
SELECT 2,'CT' UNION ALL
SELECT 2,'EN' UNION ALL
SELECT 2,'AY'
结果为
id
----------- ----------------------------
1 A,B,AE,CD
2 BW,CT,EN,AY
(2 行受影响)
Code
create TABLE t (ID INT,[name] VARCHAR(2))
INSERT INTO T
SELECT 1,'A' UNION ALL
SELECT 1,'B' UNION ALL
SELECT 1,'AE' UNION ALL
SELECT 1,'CD' UNION ALL
SELECT 2,'BW' UNION ALL
SELECT 2,'CT' UNION ALL
SELECT 2,'EN' UNION ALL
SELECT 2,'AY'
create function F_ID(@id nvarchar(100))
returns nvarchar(100)
as
begin
declare @S nvarchar(100)
select @S=isnull(@S+',','')+name from T where id=@id
return @S
end
select id, dbo.f_id(id) from t group by id
ssssssssssssssssssssssssssssssss
表table1:
┍--------------------------┐
| ID | name |
|--------------------------|
| 1 | A |
| 2 | B |
| 3 | AE |
| 4 | CD |
| 5 | BW |
| 6 | CT |
| 7 | EN |
| 8 | AY |
| . | . |
| . | . |
| . | . |
查询结果是:
┍--------------------------┐
| ID | name |
|--------------------------|
| 1 | A,AE,AY··· |
| 2 | B,BW··· |
| 3 | CD,CT··· |
| 4 | EN··· |
| . | . |
| . | . |
| . | . |
└--------------------------┚
Code
--> 生成测试数据: @T
create TABLE t (ID INT,[name] VARCHAR(2))
INSERT INTO T
SELECT 1,'A' UNION ALL
SELECT 2,'B' UNION ALL
SELECT 3,'AE' UNION ALL
SELECT 4,'CD' UNION ALL
SELECT 5,'BW' UNION ALL
SELECT 6,'CT' UNION ALL
SELECT 7,'EN' UNION ALL
SELECT 8,'AY'
create function F_Str(@name nvarchar(100))
returns nvarchar(100)
as
begin
declare @S nvarchar(100)
select @S=isnull(@S+',','')+name from T where name like left(@name,1)+'%'
return @S
end
select distinct (dbo.F_str([name]) ) [name] into #t2 from t
Select identity(int,1,1) as iid,* into #tmp from #t2
select * from #tmp
如果
create TABLE t (ID INT,[name] VARCHAR(2))
INSERT INTO T
SELECT 1,'A' UNION ALL
SELECT 1,'B' UNION ALL
SELECT 1,'AE' UNION ALL
SELECT 1,'CD' UNION ALL
SELECT 2,'BW' UNION ALL
SELECT 2,'CT' UNION ALL
SELECT 2,'EN' UNION ALL
SELECT 2,'AY'
结果为
id
----------- ----------------------------
1 A,B,AE,CD
2 BW,CT,EN,AY
(2 行受影响)
Code
create TABLE t (ID INT,[name] VARCHAR(2))
INSERT INTO T
SELECT 1,'A' UNION ALL
SELECT 1,'B' UNION ALL
SELECT 1,'AE' UNION ALL
SELECT 1,'CD' UNION ALL
SELECT 2,'BW' UNION ALL
SELECT 2,'CT' UNION ALL
SELECT 2,'EN' UNION ALL
SELECT 2,'AY'
create function F_ID(@id nvarchar(100))
returns nvarchar(100)
as
begin
declare @S nvarchar(100)
select @S=isnull(@S+',','')+name from T where id=@id
return @S
end
select id, dbo.f_id(id) from t group by id
ssssssssssssssssssssssssssssssss
本人在长沙, 有工作可以加我QQ4658276