sql server根据表中数据生成insert语句

-- ======================================================
--根据表中数据生成insert语句的存储过程
--建立存储过程,执行spGenInsertSQL 表名-- ======================================================
CREATE   proc [dbo].[spGenInsertSQL]
(@tablename varchar(256))
as
begindeclare @sql varchar(8000)declare @sqlValues varchar(8000)set @sql =' ('set @sqlValues = 'values (''+'select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + '[' + name + '],' from (select case when xtype in (48,52,56,59,60,62,104,106,108,122,127)                                then 'case when ['+ name +'] is null then ''NULL'' else ' + 'cast(['+ name + '] as varchar)'+' end'when xtype in (58,61)then 'case when ['+ name +'] is null then ''NULL'' else '+''''''''' + ' + 'cast(['+ name +'] as varchar)'+ '+'''''''''+' end'when xtype in (167)then 'case when ['+ name +'] is null then ''NULL'' else '+''''''''' + ' + 'replace(['+ name+'],'''''''','''''''''''')' + '+'''''''''+' end'when xtype in (231)then 'case when ['+ name +'] is null then ''NULL'' else '+'''N'''''' + ' + 'replace(['+ name+'],'''''''','''''''''''')' + '+'''''''''+' end'when xtype in (175)then 'case when ['+ name +'] is null then ''NULL'' else '+''''''''' + ' + 'cast(replace(['+ name+'],'''''''','''''''''''') as Char(' + cast(length as varchar)  + '))+'''''''''+' end'when xtype in (239)then 'case when ['+ name +'] is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace(['+ name+'],'''''''','''''''''''') as Char(' + cast(length as varchar)  + '))+'''''''''+' end'else '''NULL'''end as Cols,namefrom syscolumns  where id = object_id(@tablename) ) T set @sql ='select ''INSERT INTO ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')'' from '+@tablename--print @sqlexec (@sql)
end-- ======================================================
--根据表中数据生成insert语句的存储过程
--建立存储过程,执行proc_insert 表名-- ======================================================
CREATE proc [dbo].[proc_insert] (@tablename varchar(256))
as
beginset nocount on--declare @tablename varchar(256)--set @tablename = 'AD'declare @sqlstr varchar(4000)declare @sqlstr1 varchar(4000)declare @sqlstr2 varchar(4000)select @sqlstr='select ''insert '+@tablenameselect @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 =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.namefrom 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 colidselect @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename--print @sqlstrexec( @sqlstr)set nocount off
end

摘自-http://www.cnblogs.com/stu-acer/archive/2010/10/24/1859803.html

Oracle根据表中数据生成insert语句

简便型

select 'insert into TableNameA values ('||''''||Column1||''''||','||''''||Column2||''''||','||''''||Column3||''''||')' From TableNameB

CREATE OR REPLACE PROCEDURE genins_file(p_table IN varchar2,p_output_folder IN VARCHAR2,p_output_file  IN VARCHAR2)IS--
  l_column_list  VARCHAR2(32767);l_value_list   VARCHAR2(32767);l_query     VARCHAR2(32767);l_cursor     NUMBER;ignore  NUMBER;l_insertline1  varchar2(32767);l_insertline2  varchar2(32767);cmn_file_handle    UTL_FILE.file_type;--
  FUNCTION get_cols(p_table VARCHAR2)RETURN VARCHAR2ISl_cols VARCHAR2(32767);CURSOR l_col_cur(c_table VARCHAR2) ISSELECT column_nameFROM  user_tab_columnsWHERE table_name = upper(c_table)ORDER BY column_id;BEGINl_cols := null;FOR rec IN l_col_cur(p_table)LOOPl_cols := l_cols || rec.column_name || ',';END LOOP;RETURN substr(l_cols,1,length(l_cols)-1);END;--
  FUNCTION get_query(p_table IN VARCHAR2)RETURN VARCHAR2ISl_query VARCHAR2(32767);CURSOR l_query_cur(c_table VARCHAR2) ISSELECT 'decode('||column_name||',null,''null'','||decode(data_type,'VARCHAR2','''''''''||'||column_name ||'||''''''''','DATE','''''''''||to_char('||column_name||',''YYYY-MM-DD HH24:MI:SS'')||''''''''',column_name) || ')' column_queryFROM user_tab_columnsWHERE table_name = upper(c_table)ORDER BY column_id;BEGINl_query := 'SELECT ';FOR rec IN l_query_cur(p_table)LOOPl_query := l_query || rec.column_query || '||'',''||';END LOOP;l_query := substr(l_query,1,length(l_query)-7);RETURN l_query || ' FROM ' || p_table;END;--
  BEGINl_column_list := get_cols(p_table);l_query    := get_query(p_table);l_cursor := dbms_sql.open_cursor;DBMS_SQL.PARSE(l_cursor, l_query, DBMS_SQL.native);DBMS_SQL.DEFINE_COLUMN(l_cursor, 1, l_value_list, 32767);ignore := DBMS_SQL.EXECUTE(l_cursor);--
  IF NOT UTL_FILE.IS_OPEN(cmn_file_handle) THENcmn_file_handle := UTL_FILE.FOPEN (p_output_folder, p_output_file, 'a',32767);END IF;LOOPIF DBMS_SQL.FETCH_ROWS(l_cursor)>0 THENDBMS_SQL.COLUMN_VALUE(l_cursor, 1, l_value_list);l_insertline1:='INSERT INTO '||p_table||' ('||l_column_list||')';l_insertline2:=' VALUES ('||l_value_list||');';UTL_FILE.put_line (cmn_file_handle, l_insertline1);UTL_FILE.put_line (cmn_file_handle, l_insertline2);ELSEEXIT;END IF;END LOOP;IF NOT UTL_FILE.IS_OPEN(cmn_file_handle) THENUTL_FILE.FCLOSE (cmn_file_handle);END IF;END;

调用它,可以把表里的数据生成(insert into 表名 ....)OS下文件的过程genins_file方法:
  
      SQL>exec genins_file('emp','/oracle/logs','insert_emp.sql');
                  |     |        |
                表名,可变   |     生成OS下文件名,可变
                       |
                  utl_file_dir路径名,不变(我设置的是/oracle/logs)
  
      SQL> exit
  
  可以在OS目录/oracle/logs下看到insert_emp.sql文件。

调用它之前,要看看数据库的初始化参数 UTL_FILE_DIR 是否已经正确地设置:
  
  SQL> show parameters utl_file_dir;
  
  可以看到该参数的当前设置。
  
  如果没有值,必须修改数据库的initsid.ora文件,将utl_file_dir 指向一个你想用PL/SQL file I/O 的路径。
  
  重新启动数据库。此参数才生效。
  
  

转载于:https://www.cnblogs.com/daihuiquan/archive/2013/03/21/2969777.html

sql server根据表中数据生成insert语句相关推荐

  1. 利用T-SQL处理SQL Server数据库表中的重复行

    Duplicate rows in a SQL Server database table can become a problem. We will see how we can find and ...

  2. columnproperty server sql_导出SQL Server数据库表中字段的说明/备注

    时 间:2013-02-18 09:09:11 作 者:摘 要:导出SQL Server数据库表中字段的说明/备注 正 文: 打开SQL企业管理器 ,找到你要导出用户表字段信息的那个数据库 ,点击工具 ...

  3. cockroachdb mysql_CockroachDB学习笔记——[译]CockroachDB中的SQL:映射表中数据到键值存储...

    CockroachDB学习笔记--[译]CockroachDB中的SQL:映射表中数据到键值存储 原文标题:SQL in CockroachDB: Mapping Table Data to Key- ...

  4. SQL Server 将一个表中字段的值复制到另一个表的字段中

    原文:SQL Server 将一个表中字段的值复制到另一个表的字段中 具体方法如下 一:update 表2 set (要插入的列名)= select 表1.某一列 from 表1 left jion ...

  5. SQL Server 用表中已有数据造数据

    从表中选择数据再插入到表中(select XXX into 与insert into XXX select的结合) 在做性能测试时需要大量的业务数据.完全从画面造数据比较费时间,使用SQL文批量插入数 ...

  6. sql server 数据库表中插入带有英文 单引号 或双引号 的数据

    sql server 数据库中经常与遇到插入各类带单引号 ' 或是 双引号 ' ' 的数据,那么应该怎么插入呢? 正常情况下: 这样插入就不会有问题: insert into table(descri ...

  7. SQL Server向表中插入数据

    一.语句 Example 1: --向学生表中插入数据 INSERT INTO STUDENT VALUES(201215121,'李勇','男',20,'CS'); INSERT INTO STUD ...

  8. SQL Server 复制表及数据的两种方法

    1.新表不存在(即复制数据的同时创建与旧表相同结构的新表): select [col1,col2,col3...] into new_table from old_table where 1=1 2. ...

  9. sql server 数据库表中增加列,增加字段,插入列,插入字段,修改列,修改字段,

    格式 --增加列 alter table 表名 add 字段名 类型 null default 默认值--给列增加注释 execute sp_addextendedproperty 'MS_Descr ...

最新文章

  1. 我是LinkedIn的SRE,我把LinkedIn搞挂了
  2. nginx反向代理取得IP地址
  3. 深入字节码操作:使用ASM和Javassist创建审核日志
  4. Windows远程桌面(mstsc)不能复制粘贴的解决办法
  5. markdown 转义字符
  6. java执行class找不到main函数_你所不知道的HelloWorld背后的执行原理
  7. HTML5: 全局属性
  8. Delphi IOS (二)
  9. centos安装软件后没有快捷键,创建桌面快捷键的方式【pycharm的快捷键】
  10. spring cloud学习笔记02
  11. Demo源码放送:打通B/S与C/S !让HTML5 WebSocket与.NET Socket公用同一个服务端!
  12. 计算机加密是什么意思,bitlocker驱动加密是什么意思?
  13. hcfax2e伺服驱动器说明书_交流伺服电机驱动器使用说明书.-共14页
  14. Wireshark分析SMTP、POP3协议
  15. Android App 安全登录认证解决方案
  16. Pycharm相对路径
  17. python做小游戏之一小迷宫游戏
  18. 360WiFi之愚见
  19. Linux C编程一站式学习笔记2
  20. RK3568-SPI

热门文章

  1. Struts2 Problem Report: No result defined for action ... and result exception
  2. linux 防火墙 iptables的简单使用
  3. 面向车、路、城,华人运通“智路”示范项目开通试运行
  4. Javascript二进制运算符的一些运用场景
  5. 面试时,如何向公司提问?
  6. CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模
  7. 第七篇 ScrollView控件
  8. 设置元素浮动的几种方式
  9. Activity生命周期(1)
  10. 关于ArcGIS Mobile回传数据中常遇到的问题整理!