程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> SqlServer查詢結果導出insert語句

SqlServer查詢結果導出insert語句

編輯:關於SqlServer

在工作中遇到過很多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腳本。

有更好的辦法的希望大家在評論區留言,一起探討!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved