• 生成表的导出语句


    --proc_insert 'pet_acation',1    
    /*    
    根据网上资料修改,完成于 2006-6-26    
    增加对自增列的支持    
    */    
    alter  proc  proc_insert  (    
    @tablename  varchar(256),    
    @keepidentity tinyint =1,-- 是否保留identity原值 1:保留 0:不保留    
    @where varchar(1000)
    )      
    as      
    begin      
               set  nocount  on      
               declare  @sqlstr  varchar(8000)      
               declare  @sqlstr1   varchar(8000)      
               declare  @sqlstr2   varchar(8000)     
               select  @sqlstr='select  ''insert  '+@tablename      
               select  @sqlstr1=''      
               select  @sqlstr2='  ('      
               select  @sqlstr1=  '  values  (  ''+'      
        
               if @keepidentity =  1   --保留identity原值    
        
               select  @sqlstr1=@sqlstr1+col+'+'',''+'  ,@sqlstr2=@sqlstr2+name  +','  from  (select  case        
    --            when  a.xtype  =173  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name  +')'+'  end'      
               when  a.xtype  =104  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(1),'+a.name  +')'+'  end'      
               when  a.xtype  =175  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')'  +  '+'''''''''+'  end'      
               when  a.xtype  =61    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'''''''''+'+'convert(varchar(23),'+a.name  +',121)'+  '+'''''''''+'  end'      
               when  a.xtype  =106  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name  +')'+'  end'      
               when  a.xtype  =62    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(23),'+a.name  +',2)'+'  end'      
               when  a.xtype  =56    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(11),'+a.name  +')'+'  end'      
               when  a.xtype  =60    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(22),'+a.name  +')'+'  end'      
               when  a.xtype  =239  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')'  +  '+'''''''''+'  end'      
               when  a.xtype  =108  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name  +')'+'  end'      
               when  a.xtype  =231  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')'  +  '+'''''''''+'  end'      
               when  a.xtype  =59    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(23),'+a.name  +',2)'+'  end'      
               when  a.xtype  =58    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'''''''''+'+'convert(varchar(23),'+a.name  +',121)'+  '+'''''''''+'  end'      
               when  a.xtype  =52    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(12),'+a.name  +')'+'  end'      
               when  a.xtype  =122  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(22),'+a.name  +')'+'  end'      
               when  a.xtype  =48    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(6),'+a.name  +')'+'  end'      
    --            when  a.xtype  =165  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name  +')'+'  end'      
               when  a.xtype  =167  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')'  +  '+'''''''''+'  end'      
               else  '''NULL'''      
               end  as  col,a.colid,a.name      
               from  syscolumns  a  where  a.id  =  object_id(@tablename)  and  a.xtype  <>189  and  a.xtype  <>34  and  a.xtype  <>35  and    a.xtype  <>36      
               )t  order  by  colid      
                   
               else  --不用保留identity原值    
                     
        select  @sqlstr1=@sqlstr1+col+'+'',''+'  ,@sqlstr2=@sqlstr2+name  +','  from  (select  case        
               --            when  a.xtype  =173  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name  +')'+'  end'      
               when  a.xtype  =104  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(1),'+a.name  +')'+'  end'      
               when  a.xtype  =175  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')'  +  '+'''''''''+'  end'      
               when  a.xtype  =61    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'''''''''+'+'convert(varchar(23),'+a.name  +',121)'+  '+'''''''''+'  end'      
               when  a.xtype  =106  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name  +')'+'  end'      
               when  a.xtype  =62    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(23),'+a.name  +',2)'+'  end'      
               when  a.xtype  =56    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(11),'+a.name  +')'+'  end'      
               when  a.xtype  =60    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(22),'+a.name  +')'+'  end'      
               when  a.xtype  =239  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')'  +  '+'''''''''+'  end'      
               when  a.xtype  =108  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name  +')'+'  end'      
               when  a.xtype  =231  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')'  +  '+'''''''''+'  end'      
               when  a.xtype  =59    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(23),'+a.name  +',2)'+'  end'      
               when  a.xtype  =58    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'''''''''+'+'convert(varchar(23),'+a.name  +',121)'+  '+'''''''''+'  end'      
               when  a.xtype  =52    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(12),'+a.name  +')'+'  end'      
               when  a.xtype  =122  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(22),'+a.name  +')'+'  end'      
               when  a.xtype  =48    then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar(6),'+a.name  +')'+'  end'      
    --            when  a.xtype  =165  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name  +')'+'  end'      
               when  a.xtype  =167  then  'case  when  '+a.name+'  is  null  then  ''NULL''  else  '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')'  +  '+'''''''''+'  end'      
               else  '''NULL'''      
               end  as  col,a.colid,a.name      
               from  syscolumns  a  where  a.id  =  object_id(@tablename)  and  a.xtype  <>189  and  a.xtype  <>34  and  a.xtype  <>35  and    a.xtype  <>36      
               and columnproperty(object_id(@tablename),a.name,'isidentity')!=1     
               )t  order  by  colid      
        
        
               select  @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+')  '+left(@sqlstr1,len(@sqlstr1)-3)+')''  from  '+@tablename  +' ' +@where
    --    print  @sqlstr      
               exec(  @sqlstr)      
        
               --如果需要保留identity原值,又有identity列时,输出 set identity_insert 语句    
               if  OBJECTPROPERTY(OBJECT_ID(@tablename),'TableHasIdentity') =1 and @keepidentity = 1    
               begin    
                   print 'set identity_insert ' + @tablename + ' on'    
               print 'set identity_insert ' + @tablename + ' off'    
               end      
               set  nocount  off      
    end      
     
  • 相关阅读:
    Scala学习笔记
    flume ng 1.3 安装(转)
    学习scala trait
    scala性能测试
    scala容器使用
    hdu 4607 Park Visit
    FZU 1591 Coral的烦恼
    fzu 1675 The Seventy-seven Problem
    hdu 4602 Partition
    zoj 1842 Prime Distance
  • 原文地址:https://www.cnblogs.com/mxw09/p/2021518.html
Copyright © 2020-2023  润新知