栀子花开

追求完美

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

select * from company as c1 inner join company c2 on c1.companyid=c2.companyid where 


  create   table   [work]([Id]   int,TypeId   int,[Name]   varchar(10))  
  insert   into   [work]   select   1,1,'3232'  
  insert   into   [work]   select   2,3,'grtr'  
  insert   into   [work]   select   3,2,'gggr'  
  insert   into   [work]   select   4,2,'yy'  
  insert   into   [work]   select   5,1,'zz'  
  insert   into   [work]   select   6,3,'g33rtr'  
 insert   into   [work]   select   7,3,'g33rtr'  
 insert   into   [work]   select   8,2,'yy'  

  create   table   Type([Id]   int,TypeNum   int)  
  insert   into   Type   select   1,3  
  insert   into   Type   select   2,1  
  insert   into   Type   select   3,2  
   

select * from work
select distinct typeid into liny  from work


select * from work  as w1  where id in (
select top 2 id from work w2 where w1.typeid=w2.typeid order by id) order by typeid 

  declare   @sql   varchar(1000)  
  set   @sql=''  
  select   @sql=@sql+'   select   TOP   '+cast(TypeNum   as   varchar)+'   b.[id],b.[typeid],b.[name]   from   type   a,work   b   where   a.[id]=b.[typeid]   and   a.id='+cast([id]   as   varchar)+'   union   all'   from   type  
  set   @sql=left(@sql,len(@sql)-11)  
  print @sql
  exec(@sql)



  declare   @sql   varchar(1000)  
  set   @sql=''  
  select   @sql=@sql+'   select   TOP   '+cast(TypeNum   as   varchar)+'   b.[id],b.[typeid],b.[name]   from   type   a,work   b   where   a.[id]=b.[typeid]   and   a.id='+cast([id]   as   varchar)+'   union   all'   from   type  
  set   @sql=left(@sql,len(@sql)-11)  
  print @sql
  exec(@sql)

posted on 2008-01-18 18:06  杨林  阅读(196)  评论(0编辑  收藏  举报