如何得到另一表中的值?
表staff(员工表)
staff_id staff_name
    1         王七蛋
     2         朱九妹
表pact(合同表)
pact_id  staff_ids  
    1        1,2
在存储过程中,如何才能得到
pact_id pact_ids staff_names
    1      1,2    王七蛋,朱九妹
------解决方案--------------------参考:
--生成测试数据
create table 表(部门 int,人员 varchar(20))
insert into 表 select 1,'张三'
insert into 表 select 1,'李四'
insert into 表 select 1,'王五'
insert into 表 select 2,'赵六'
insert into 表 select 2,'邓七'
insert into 表 select 2,'刘八'
go
--创建用户定义函数
create function f_str(@department int)
returns varchar(8000)
as
begin
   declare @ret varchar(8000)
   set @ret = ''
   select @ret = @ret+','+人员 from 表 where 部门 = @department
   set @ret = stuff(@ret,1,1,'')
   return @ret  
end
go
--执行
select 部门,人员=dbo.f_str(部门) from 表 group by 部门 order by 部门
go
--输出结果
/*
部门  人员
----  --------------
1     张三,李四,王五
2     赵六,邓七,刘八
*/
--删除测试数据
drop function f_str
drop table 表
go
------解决方案--------------------参阅
tba
ID  classid   name
1     1,2,3   西服  
2     2,3    中山装
3     1,3    名裤
tbb  
id   classname
1     衣服
2     上衣
3     裤子
我得的结果是
id   classname            name
1     衣服,上衣,裤子      西服  
2          上衣,裤子     中山装
3     衣服,裤子          名裤
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           衣服,裤子      名裤
(所影响的行数为 3 行)
*/