zoukankan      html  css  js  c++  java
  • 生成表的导出语句

    --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      
     
  • 相关阅读:
    Linux 学习 step by step (1)
    ubuntu server nginx 安装与配置
    ubuntu server samba服务器配置
    iOS app集成支付宝支付流程及后台php订单签名处理
    mac 连接windows 共享内容
    linux 文件查找,which,whereis,locate,find
    ubuntu server vsftpd 虚拟用户及目录
    ubuntu server 安装 mantis bug tracker 中文配置
    ubuntu server vsftpd 匿名用户上传下载及目录设置
    linux 用户管理,用户权限管理,用户组管理
  • 原文地址:https://www.cnblogs.com/mxw09/p/2021518.html
Copyright © 2011-2022 走看看