1.两张结构相同的表,使他们的数据同步
      
      create table #A(id int, y vachar(10))
      create table #B(id int, y varchar(10))
      
      insert into #A 
         select 1, 'a1'
         union
         select 2, 'a2'
      select * from #A
      
      insert into #B
         select 1, 'b1'
         union
         select 2, 'b2'
      select * from #B

      update #B set y=#A.y from #B,#A where #A.id=#B.id
      
      select * from #B
      
      drop table #A
      drop table #B

   2.对于一张表的一列,查找出不重复的记录

      create table #A(x int,y varchar(10))
      
      insert into #A 
         select 1, '张三'
         union  
         select 2, '张三'
         union
         select 3, '李四'

      select distinct y,count(y) [count] from #A group by y

      drop table #A

   3.将表A查询成下面的样子
表A

id    x
1    abc
2    xyz
3    abc
4    abc

B
abc  xyz
3     1  

   select * into #temp from( select
      1 id,    'abc' x
      union
      select 2,    'xyz'
       union
      select 3,    'abc'
      union
      select 4,    'abc'
      ) A

     select * from #temp
    declare @sql varchar(8000)
      set @sql = 'select '

    select @sql = @sql + ltrim(str(f2)) + ' ' + f1 + ', ' from 
   (
       select x f1, count(*) f2 from #temp group by x
    ) A


     set @sql =  substring(@sql, 1, len(@sql) - 1)

   select @sql

   exec( @sql )

   drop table #temp

      

posted on 2007-03-28 14:12  qy  阅读(505)  评论(1编辑  收藏  举报