USE [db]
GO
/****** Object: StoredProcedure [dbo].[proc_insert] Script Date: 12/05/2012 17:18:31 ******/
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(4000)
declare @sqlstr1 varchar(4000)
declare @sqlstr2 varchar(4000)
select @sqlstr='select ''insert '+@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

执行 Exec proc_insert TableName

结果如下

摘自:http://blog.csdn.net/hexin373/article/details/8260752

把MSSQL的表数据查询成 insert into格式的函数的更多相关文章

  1. Pl/sql 如何将oracle的表数据导出成excel文件?

    oracle将表数据导出成excel文件的方法 1)在SQL窗体上,查询需要导出的数据 --查询数据条件-- ; 结果视图 2)在查询结果的空白处,右键选择Copy to Excel 3) 查看导出e ...

  2. 10Oracle Database 数据表数据查询

    Oracle Database 数据表数据查询 DML 数据操纵语言 - 数据的查看和维护 select / insert /delete /update 基本查询语句 Select [distinc ...

  3. hive 将hive表数据查询出来转为json对象和json数组输出

    一.将hive表数据查询出来转为json对象输出 1.将查询出来的数据转为一行一行,并指定分割符的数据 2.使用UDF函数,将每一行数据作为string传入UDF函数中转换为json再返回 1.准备数 ...

  4. SQL Server 的表数据简单操作(表数据查询)

    --表数据查询----数据的基本查询-- --数据简单的查询--select * | 字段名[,字段名2, ...] from 数据表名 [where 条件表达式] 例: use 商品管理数据库 go ...

  5. Java将其他数据类型转换成JSON字符串格式

    Student.java package com.demo.servlet; import java.util.List; import java.util.Map; public class Stu ...

  6. MySQL多表数据查询(DQL)

    数据准备: /* ------------------------------------创建班级表------------------------------------ */ CREATE TAB ...

  7. MySQL单表数据查询(DQL)

    数据准备工作: CREATE TABLE student( sid INT PRIMARY KEY AUTO_INCREMENT, sname ), age TINYINT, city ), scor ...

  8. MySQL学习总结(五)表数据查询

    查询数据记录,是指从数据库对象表中获取所要查询的数据记录,该操作可以说是数据最基本的操作之一,也是使用频率最高.最重要的数据操作. 1.单表数据记录查询 1.1.简单数据查询 SELECT field ...

  9. Oracle数据库字段数据拆分成多行(REGEXP_SUBSTR函数)

    做多选功能时为了简便,会在某个字段中存储多个值,保存时虽然省事,但后续的查询统计时还需要拆分数据才行,因此这时需要将字段内的值分成多行以便后续使用. 下面这个例子实现了字段内数据的拆分: --创建测试 ...

随机推荐

  1. Python基础学习7---异常处理

    处理异常 我们可以使用 try..except 语句来处理异常.我们把通常的语句放在try-块中,而把我们的错误处理语句放在except-块中. import sys try: s = raw_inp ...

  2. svn hooks的使用demo

    我是理论家: svn server端提供了Hooks Script.所谓钩子实际上是一种时间触发机制,是指当系统执行到某个特殊的事件时,触发我们预定义的动作,可以让我们在某些特定状态发生的时候做我们想 ...

  3. Qt - 设置TableWidget只读

    ui->infoViewTW->setEditTriggers(QAbstractItemView::NoEditTriggers); enum QAbstractItemView::Ed ...

  4. js 时间

    <html> <head> <meta charset="utf-8" /> <title></title> <s ...

  5. Java中try-catch-finally的一点理解

    在只有try-catch语句中,如果catch块中出现了return语句或者抛出了异常,那么catch之后的语句是执行不到的:但是如果将代码放入finally中,即使catch中出现了return语句 ...

  6. hdu_2227_Find the nondecreasing subsequences_树状数组,离散化

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 题意:给你一个集合,让你求递增子序列有多少个,和树状数组求逆序对差不多,不过数据比较大,要离散化 ...

  7. php中header函数参数的 Cache-control:private,no-cache,must-revalidate,max-age 使用方法

    网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private.no-cache.max-age.must-revalidate等,默认为private.其作用根据 ...

  8. 笨方法学python--简介

    该章提到编程新手所需的三种最重要的技术:读和写,注重细节,发现不同. 读 和 写 即熟悉代码中的各种字符. 注 重 细 节 将例子一字不差地打出来,通过实践训练自己 发 现 不 同 这个是通过长年累月 ...

  9. EGL接口介绍-----Android OpenGL ES底层开发

    引自:http://www.cnitblog.com/zouzheng/archive/2011/05/30/74326.html EGL 是 OpenGL ES 和底层 Native 平台视窗系统之 ...

  10. 青蛙的约会<数论,extgcd>

    青蛙的约会 题意: 在一个圆上有一个零刻度点,公青蛙和母青蛙分别在A点和B点<不同的位 置>,他们每秒行走的距离分别是m和n,圆的周长是L.问题是这两个青 蛙能不能相遇,若能在什么时候相遇 ...