国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

導出表數據腳本的存儲過程

2024-07-21 02:43:33
字體:
來源:轉載
供稿:網友

生成表數據腳本的存儲過程
CREATE PROCEDURE   dbo.OutputData  
  @tablename   sysname      
  AS      
  declare   @column   varchar(1000)     
  declare   @columndata   varchar(1000)     
  declare   @sql   varchar(4000)     
  declare   @xtype   tinyint      
  declare   @name   sysname      
  declare   @objectId   int      
  declare   @objectname   sysname      
  declare   @ident   int      

  set   nocount   on      
  set   @objectId=object_id(@tablename)     
  if  @objectId   is   null   --   判斷對象是否存在     
     begin      
      print  @tablename +  '對象不存在'     
      return      
    end     
  set @objectname=rtrim(object_name(@objectId))     
  if @objectname is null or charindex(@objectname,@tablename)=0
    begin      
      print  @tablename +  '對象不在當前數據庫中'      
      return      
    end         
  if  OBJECTPROPERTY(@objectId,'IsTable')    <   >    1   --   判斷對象是否是表     
    begin      
      print  @tablename +  '對象不是表'     
      return      
    end         
  select   @ident=status&0x80   from   syscolumns   where   id=@objectid   and   status&0x80=0x80          
  if @ident is   not   null      
    print   'SET   IDENTITY_INSERT   '+ @TableName + '   ON'     
  --定義游標,循環取數據并生成Insert語句
  declare  syscolumns_cursor  cursor for   
    select   c.name,c.xtype   from   syscolumns   c     
      where   c.id=@objectid     
      order   by   c.colid     
  --打開游標
  open   syscolumns_cursor     
  set  @column=''     
  set  @columndata=''     
  fetch   next   from   syscolumns_cursor   into   @name,@xtype     
  while   @@fetch_status    <>  -1     
    begin      
    if   @@fetch_status    <>  -2     
      begin      
      if   @xtype   not   in(189,34,35,99,98)   --timestamp不需處理,image,text,ntext,sql_variant 暫時不處理     
        begin      
        set   @column=@column + 
          case   when   len(@column)=0   then ''  
                 else   ','
                 end + @name     
        set   @columndata = @columndata + 
          case   when   len(@columndata)=0   then   ''   
                 else   ','','','
                 end  + 
          case   when  @xtype   in(167,175)  then   '''''''''+'+@name+'+'''''''''                --varchar,char     
                 when   @xtype   in(231,239)   then   '''N''''''+'+@name+'+'''''''''             --nvarchar,nchar     
                 when   @xtype=61   then   '''''''''+convert(char(23),'+@name+',121)+'''''''''   --datetime     
                 when   @xtype=58   then   '''''''''+convert(char(16),'+@name+',120)+'''''''''   --smalldatetime     
                  when   @xtype=36   then   '''''''''+convert(char(36),'+@name+')+'''''''''       --uniqueidentifier     
                 else   @name   
                 end      
        end      
      end      
    fetch   next   from   syscolumns_cursor   into   @name,@xtype     
    end      
  close   syscolumns_cursor     
  deallocate   syscolumns_cursor          
  set  @sql='set   nocount   on   select   ''insert   '+@tablename+'('+@column+')   values(''as   ''--'','+@columndata+','')''   from   '+@tablename         
  print   '--'+@sql     
  exec(@sql)          
  if   @ident   is   not   null      
  print  'SET   IDENTITY_INSERT   '+@TableName+'   OFF'     
 

 exec   OutputData2   '表名'


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 赤壁市| 共和县| 沙雅县| 陕西省| 逊克县| 印江| 扶绥县| 手机| 汶上县| 乐昌市| 当阳市| 皋兰县| 航空| 南丹县| 富裕县| 交口县| 兰坪| 临沭县| 延安市| 修武县| 乡宁县| 连平县| 卓尼县| 盘锦市| 梧州市| 花莲县| 封开县| 成都市| 陈巴尔虎旗| 姜堰市| 蕉岭县| 柳河县| 深水埗区| 扎囊县| 邵阳县| 平泉县| 浦城县| 怀来县| 万荣县| 鹤山市| 内乡县|