sky_blue
最近评论
文章分类
    收藏
      相册
      存档
      软件项目交易
      订阅我的博客
      XML聚合  FeedSky
      订阅到鲜果
      订阅到Google
      订阅到抓虾
      订阅到BlogLines
      订阅到Yahoo
      订阅到GouGou
      订阅到飞鸽
      订阅到Rojo
      订阅到newsgator
      订阅到netvibes

      原创 转置的存储过程收藏

      新一篇: 生成插入语句的存储过程 | 

       

      create       proc       proc_sky_blue       (@tablename       varchar(200))  

      as  

      begin  

              set       nocount       on  

              declare       @col       nvarchar(256)  

              declare       @makesql       nvarchar(4000)  

              declare       @insertsql       nvarchar(4000)  

              declare       @caculatesql       nvarchar(400)  

              declare       @count       int  

              declare       @i       int  

              create       table       #tmp       (colname       nvarchar(20))  

              select       @caculatesql       =       'select       @count=count(1)       from       '       +       @tablename  

              exec       sp_executesql       @caculatesql,       N   '@count       int       output   ',@count       output  

              if       @count       >   =1024  

              begin  

                      raiserror(   '表的行数太多了,我转不了   ',16,1)  

              end  

              else  

              begin  

                      select       @i=0  

                      while       @count       >   0  

                      begin  

                              select       @i=@i+1  

                              select       @makesql       =       'alter       table       #tmp       add       col   '+convert(varchar(20),@i)+   '       int   '  

                              exec(@makesql)  

                              select       @count=@count-1  

                      end  

                      declare       my_cursor       cursor       for  

                      select       name       from       syscolumns       where       order       by       colid  

                      open       my_cursor  

                      fetch       next       from       my_cursor       into       @col  

                      while       @@fetch_status       =       0  

                      begin  

                              select       @makesql       =   'select       @insertsql=       @insertsql       +       convert(varchar(4),   '+@col+   ')       +   '   ',   '   '       from       '       +@tablename  

                              select       @insertsql       =N   'insert       #tmp       values       (   '   '   '+@col+       '   '   ',   '  

                              execute       sp_executesql       @makesql,N   '@insertsql       nvarchar(4000)       output   '       ,@insertsql       output  

                              select       @insertsql       =       left(@insertsql,len(@insertsql)-1)       +   ')   '  

                              exec(@insertsql)  

                              fetch       next       from       my_cursor       into       @col  

                      end  

                      close       my_cursor  

                      deallocate       my_cursor  

                      select       *       from       #tmp  

                      set       nocount       off  

              end  

      end   
       

      发表于 @ 2007年12月30日 20:05:00|评论(loading...)|编辑

      新一篇: 生成插入语句的存储过程 | 

      评论:没有评论。

      发表评论  


      当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
      Csdn Blog version 3.1a
      Copyright © sky_blue