--简单insert语句
create proc spGenInsertSQL
@TableName as varchar(100)
as
--declare @TableName varchar(100)
--set @TableName = 'orders'
--set @TableName = 'eeducation'
DECLARE xCursor CURSOR FOR
SELECT name,xusertype
FROM syscolumns
WHERE (id = OBJECT_ID(@TableName))
declare @F1 varchar(100)
declare @F2 integer
declare @SQL varchar(8000)
set @sql ='SELECT ''INSERT INTO ' + @TableName + ' VALUES('''
OPEN xCursor
FETCH xCursor into @F1,@F2
WHILE @@FETCH_STATUS = 0
BEGIN
set @sql =@sql +
+ case when @F2 IN (35,58,99,167,175,231,239,61) then ' + case when ' + @F1 + ' IS NULL then '''' else '''''''' end + ' else '+' end
+ 'replace(ISNULL(cast(' + @F1 + ' as varchar(8000)),''NULL''),'''''''','''''''''''')'
+ case when @F2 IN (35,58,99,167,175,231,239,61) then ' + case when ' + @F1 + ' IS NULL then '''' else '''''''' end + ' else '+' end
+ char(13) + ''','''
FETCH NEXT FROM xCursor into @F1,@F2
END
CLOSE xCursor
DEALLOCATE xCursor
set @sql = left(@sql,len(@sql) - 5) + ' + '')'' FROM ' + @TableName
exec (@sql)
go
EXEC spGenInsertSQL basorgteam
--完整insert语句(多列)
CREATE PROCEDURE dbo.OutputData
@tablename sysname
AS
declare @column varchar(1000)
declare @columndata varchar(1000)
declare @sql varchar(4000)
declare @xtype tinyint
declare @name sysname
declare @objectId int
declare @objectname sysname
declare @ident int
set nocount on
set @objectId=object_id(@tablename)
if @objectId is null -- 判断对象是否存在
begin
print @tablename + '对象不存在'
return
end
set @objectname=rtrim(object_name(@objectId))
if @objectname is null or charindex(@objectname,@tablename)=0
begin
print @tablename + '对象不在当前数据库中'
return
end
if OBJECTPROPERTY(@objectId,'IsTable') < > 1 -- 判断对象是否是表
begin
print @tablename + '对象不是表'
return
end
select @ident=status&0x80 from syscolumns where id=@objectid and status&0x80=0x80
if @ident is not null
print 'SET IDENTITY_INSERT '+ @TableName + ' ON'
--定义游标,循环取数据并生成Insert语句
declare syscolumns_cursor cursor for
select c.name,c.xtype from syscolumns c
where c.id=@objectid
order by c.colid
--打开游标
open syscolumns_cursor
set @column=''
set @columndata=''
fetch next from syscolumns_cursor into @name,@xtype
while @@fetch_status <> -1
begin
if @@fetch_status <> -2
begin
if @xtype not in(189,34,35,99,98) --timestamp不需处理,image,text,ntext,sql_variant 暂时不处理
begin
set @column=@column +
case when len(@column)=0 then ''
else ','
end + @name
set @columndata = @columndata +
case when len(@columndata)=0 then ''
else ','','','
end +
case when @xtype in(167,175) then '''''''''+'+@name+'+''''''''' --varchar,char
when @xtype in(231,239) then '''N''''''+'+@name+'+''''''''' --nvarchar,nchar
when @xtype=61 then '''''''''+convert(char(23),'+@name+',121)+''''''''' --datetime
when @xtype=58 then '''''''''+convert(char(16),'+@name+',120)+''''''''' --smalldatetime
when @xtype=36 then '''''''''+convert(char(36),'+@name+')+''''''''' --uniqueidentifier
else @name
end
end
end
fetch next from syscolumns_cursor into @name,@xtype
end
close syscolumns_cursor
deallocate syscolumns_cursor
set @sql='set nocount on select ''insert '+@tablename+'('+@column+') values(''as ''--'','+@columndata+','')'' from '+@tablename
print '--'+@sql
exec(@sql)
if @ident is not null
print 'SET IDENTITY_INSERT '+@TableName+' OFF'
--执行存储过程语句
exec OutputData functionButtonInfo
--完整insert语句(单列,每列的长度最大为256,如果insert语句超过256长度的话,就不要用这个)
create proc proc_insert (@tablename varchar(256))
as
begin
set nocount on
declare @sqlstr nvarchar(10000)
declare @sqlstr1 nvarchar(10000)
declare @sqlstr2 nvarchar(10000)
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 =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
go
exec proc_insert functionButtonInfo
分享到:
相关推荐
1. 存储过程只在创造时进行编译即可,以后每次执行存储过程都不需再重新编译,而我们通常使用的SQL语句每执行一次就编译一次,所以使用存储过程可提高数据库执行速度。 2. 经常会遇到复杂的业务逻辑和对数据库的...
主要介绍了SQL Server使用存储过程生成insert语句再执行大量插入数据的方法
sqlserver的存储过程批量生成insert插入语句 在需要批量导入数据或者保留数据的情况下使用
功能: 根据表名、where条件,生成导出数据的SQL语句。(包含insert语句。结果可一键执行,利于数据导出、导入) 参数: @tableName nvarchar(100) --表名 ,@sqlWhere nvarchar(500) --where条件(传空时,导出全部...
存储过程生成器源代码,可自动生成所有存储过程 适合sql server 2000,可自动生成INSERT,SELECT,UPDATE等操作的存储过程,免去手工写的辛苦. 是VS2005的项目源码.
导出sql server表数据(生成以insert插入的语句方式的存储过程)
在SQL Server中生成INSERT..VALUES语句的存储过程。
创建一个监控表,一个被调用的存储过程即可,推荐有存储过程编广泛使用的程序使用 调用方法 DECLARE @PKID CHAR(12)='' EXEC [dbo].[SysGetObjectPKId] @ObjectName = '你的表名称',@PKID = @PKID OUTPUT insert 你的...
在Sql server里面运行改sql脚本生成存储过程,运行存储过程可以把表里面记录导出insert语句。
其中一个用于生成Insert过程,另一个用于生成Update过程。 Sp_GenInsert 该过程运行后,它为给定的表生成一个完整的Insert过程。如果原来的表有标识列,您得将生成的过程中的SET IDNTITY_INSERT ON 语句手工...
SQL Server的导入导出功能可以导出创建数据库各对象的脚本,却不提供导出目标表的现有数据为Insert语句的功能,网上也有此类存储过程,但要运行还得给客户数据库上create它,昨天兴致突来,写了一个工具ExpertSQL,...
sql语句自动生成器可连接sql server数据库,根据选择的数据库获取所有表,用户可以选择表或者选择表的某些字段进行自动生成Insert,select,update,delete语句及存储过程,你还可以自定义设置各种语句结构,使用非常...
由于sql server2005里没有像oracle那样将数据导出成sql语句(insert into)的功能。所以,写的一个存储过程,该存储过程将提成的表的数据全部生成insert into.....这样的插入语句。以便数据的复制。
SQL Server 数据库管理常用的SQL和T-SQL语句 SQL SERVER 与ACCESS、EXCEL的数据转换 sql server中,日期比较、日期查询的...根据表中数据生成insert语句的存储过程.txt 精妙的SQL语句.txt 事务处理.txt php_ADODB.txt
介绍就不多说了,下边是部分目录,觉得有用的话就顶一个 C:. │ sqlserver2000.txt │ ├─第01章 │ 1.9.1 设置内存选项.sql │ 1.9.2(2) 使用文件及文件组.sql │ 1.9.2(3) 调整...
4.3 通过存储过程配置SQL Server 4.3.1 使用SQL Server Management Studio查询 4.3.2 执行查询和改变设置 4.3.3 检查和设置配置参数 4.3.4 使用ALTER DATABASE改变设置 第Ⅱ部分 SQL Server 2008的系统管理 第5章 ...
6.1 SQL Server存储 6.2 理解索引 6.3 创建、修改和删除索引 6.4 明智地决定何时何地使用何种索引 6.5 维护索引 6.6 小结 第7章 更高级的索引结构 7.1 XML索引 7.2 用户定义的数据类型 ...
4.3 通过存储过程配置SQL Server 4.3.1 使用SQL Server Management Studio查询 4.3.2 执行查询和改变设置 4.3.3 检查和设置配置参数 4.3.4 使用ALTER DATABASE改变设置 第Ⅱ部分 SQL Server 2008的系统管理 第5章 ...