zoukankan      html  css  js  c++  java
  • SqlServer查询结果导出insert语句

    在工作中遇到过很多Sql server的数据需要导入到Oracle的,Sqlserver原本是自带导入Oracle的配置管理工具的(具体可百度sqlserver导入Oracle,有很多文档),但是由于效率问题被很多人遗弃了,这里介绍一个将Sqlserver的查询结果生成insert语句的方法,见下文。

    1、编写sql server生成insert 语句的存储过程:

    USE [SHMAP]
    GO
    /****** Object:  StoredProcedure [dbo].[proc_insert]    Script Date: 2016/4/13 15:33:41 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
     ALTER proc [dbo].[proc_insert] (@tablename varchar(256))
     as
     begin
     set nocount on
     declare @sqlstr varchar(8000)
     declare @sqlstr1 varchar(8000)
     declare @sqlstr2 varchar(8000)
     select @sqlstr='select ''insert into '+@tablename 
     select @sqlstr1=''
     select @sqlstr2=' ('
     select @sqlstr1= ' values ( ''+'
     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 =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(20),'+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
    
     select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+');'' from '+@tablename
     -- print @sqlstr
     exec( @sqlstr)
     set nocount off
     end

    2、执行存储过程获取insert语句:

    exec proc_insert 'SUP_UNIT';

    3 、得出insert语句如下:

    insert into SUP_UNIT (ID,NAME,PARENTID,ABBREVIATION,STATUS,ADDRESS,ZIP,TELEPHONE)  values ( 1,'巴士集团',NULL,'巴士集团','0','','','');
    insert into SUP_UNIT (ID,NAME,PARENTID,ABBREVIATION,STATUS,ADDRESS,ZIP,TELEPHONE)  values ( 2,'浦东公交',NULL,'浦东公交','0','','','');
    insert into SUP_UNIT (ID,NAME,PARENTID,ABBREVIATION,STATUS,ADDRESS,ZIP,TELEPHONE)  values ( 3,'上海巴士电车有限公司',1,'巴士电车','0','卢湾区淮海中路398号','200093','65660600');
    insert into SUP_UNIT (ID,NAME,PARENTID,ABBREVIATION,STATUS,ADDRESS,ZIP,TELEPHONE)  values ( 4,'上海巴士二汽公共交通有限公司',1,'巴士二汽','0','东诸安浜路231号505室-H室','200050','64925849');

     PS:如果insert数据量较大可以全复制然后保存在sql文件中,使用SQLPLUS命令执行sql脚本。

    有更好的办法的希望大家在评论区留言,一起探讨!

  • 相关阅读:
    PB中的函数ProfileString
    PB做大屏显示滚动窗口 [转载]
    PB对象(sqlca、sqlsa、sqlda)[转载]
    SQL中 EXCEPT、INTERSECT用法[转载]
    第一篇博客
    Intelli IDEA 炫酷插件
    概念总结
    秒杀系统(四)——异常处理和常量的处理
    秒杀系统(三)——实现哪些秒杀系统功能
    秒杀系统(二)
  • 原文地址:https://www.cnblogs.com/zoufan/p/5387717.html
Copyright © 2011-2022 走看看