create table tba(ID int,classid varchar(20),name varchar(10))
insert into tba values(1, '1,2,3 ', '西服 ')
insert into tba values(2, '2,3 ' , '中山装 ')
insert into tba values(3, '1,3 ' , '名裤 ')
create table tbb(ID varchar(10), classname varchar(10))
insert into tbb values( '1 ', '衣服 ')
insert into tbb values( '2 ', '上衣 ')
insert into tbb values( '3 ', '裤子 ')
go --第1种方法,创建函数来显示
create function f_hb(@id varchar(10))
returns varchar(1000)
as
begin
declare @str varchar(1000)
set @str= ' '
select @str=@str+ ', '+[classname] from tbb where charindex( ', '+cast(id as varchar)+ ', ', ', '+@id+ ', ') >0
return stuff(@str,1,1, ' ')
end
go
select id,classid=dbo.f_hb(classid),name from tba
drop function f_hb
我只能得到
id classid name
----------- ------------- ----------
1 西服
2 中山装
3 名裤
我想得到
id classid name
----------- ------------- ----------
1 衣服,上衣,裤子 西服
2 上衣,裤子 中山装
3 衣服,裤子 名裤 谢谢大家
insert into tba values(1, '1,2,3 ', '西服 ')
insert into tba values(2, '2,3 ' , '中山装 ')
insert into tba values(3, '1,3 ' , '名裤 ')
create table tbb(ID varchar(10), classname varchar(10))
insert into tbb values( '1 ', '衣服 ')
insert into tbb values( '2 ', '上衣 ')
insert into tbb values( '3 ', '裤子 ')
go --第1种方法,创建函数来显示
create function f_hb(@id varchar(10))
returns varchar(1000)
as
begin
declare @str varchar(1000)
set @str= ' '
select @str=@str+ ', '+[classname] from tbb where charindex( ', '+cast(id as varchar)+ ', ', ', '+@id+ ', ') >0
return stuff(@str,1,1, ' ')
end
go
select id,classid=dbo.f_hb(classid),name from tba
drop function f_hb
我只能得到
id classid name
----------- ------------- ----------
1 西服
2 中山装
3 名裤
我想得到
id classid name
----------- ------------- ----------
1 衣服,上衣,裤子 西服
2 上衣,裤子 中山装
3 衣服,裤子 名裤 谢谢大家
CSDN这个SB控件自己加了空格.