使用SQL Server 自带的“生成SQL脚本”工具,可以生成创建表、视图、存储过程等的SQL脚本。那么,能否将表中的数据也生成为SQL脚本,在查询分析器中执行这些脚本后自动将数据导入到SQL Server中呢?答案是肯定的,网上收集资料如下:
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_cursorcursor 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 'myuser' 其中myUser中当前数据库中存在的表。
另外方丈的:
drop proc proc_insert
go
create proc 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 =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
drop proc proc_insert
go
create proc 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 =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
---------------------------------------------------------------------------------------
小干部的:
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),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
print @sql
exec (@sql)
分享到:
相关推荐
可以将数据库中的数据导出insert into脚本, sql server2008以上版本已经自带导出数据库数据成insert脚本功能,该工具针对2005以下版本.使用时先选择数据库服务器地址,在右侧选择Sql Server login,输入数据库账号...
一款可自定义条件导出数据工具。SQLSERVER版数据库Insert语句生成工具。 因SQL server management studio及其它三方数据库管理工具无法做到导出SQL server数据库指定条件的数据。所以小编介绍一种python编写的脚本。
可以根据表内容自动生成insert 脚本,select 脚本,update脚本; blob和text数据类型的字段不支持。 可以方便定制需要的字段和条件。 维护SQL server 十分方便的小工具。 在不同数据库或服务器之间传递数据非常...
生成数据库表insert语句脚本,可将某张表的内容自动生成对应的insert语句
小工具,我有时用这个来给数据库中表里面的数据行生成Insert脚本,给软件升级时,制作sql更新脚本时会用到。
当前支持oracle、sybase、mysql和ms sqlServer数据库。 你可以修改参数文件、增加新的jdbc驱动来增加新数据库种类的支持。 使用方法: 软件为免安装的绿色软件。系统必须已经安装1.4版本以上的java虚拟机。 解...
SQL Server的导入导出功能可以导出创建数据库各对象的脚本,却不提供导出目标表的现有数据为Insert语句的功能,网上也有此类存储过程,但要运行还得给客户数据库上create它,昨天兴致突来,写了一个工具ExpertSQL,...
PyCharm生成数据库表结构语句脚本以及数据库数据insert语句的方法.docx
如下是一个简化的员工考勤应用E-R图,请在SQL Server中创建名为YQKG的数据库,包括两个数据文件,一个日志文件,文件名按SQL Server对象命名规范定义,数据文件按10%的比例增长,数据库定义完成后输入如下样本数据。...
本版本只支持Sqlserver数据库 如果需要将在后期版本中增加对其他数据库的支持 谢谢使用,如有其他需求请通过邮件和QQ的方式联系
使用CodeSmith进行简单的属性配置,即可批量生成表的创建脚本(sqlserver)
生成insert 语句,方便从一个数据库,服务器导出表。
一个同事(高手)写的; 支持SQL Server,Oracle ; 可以自动生成insert语句或update语句; 支持Python脚本调试; 总之非常好用,不要错过哦!!!!!
编写这个小工具,是因为平时部署项目的时候,需要导出一些公共的数据(权限、参数设置等),覆盖插入正式环境。 话不多说,直接上代码: import pyodbc import warnings import decimal import winreg import os ...
支持Microsoft SQL Server 数据库自动生成insert 插入sql脚本,原创,不足请指导
下边是部分目录,觉得有用的话就顶一个 C:. │ sqlserver2000.txt │ ├─第01章 │ 1.9.1 设置内存选项.sql │ 1.9.2(2) 使用文件及文件组.sql │ 1.9.2(3) 调整tempdb数据库的文件...
此工具用delphi开发,它是一个通用的SQL数据导出工具,并且可以将表里的每一条数据生成相应的insert脚本,利用导出的脚本,可以很方便的实现将一个表的数据导入到另外一个数据库中
2.1.3 使用SQL Server 2008的多维数据库和数据挖掘 2.1.4 使用SQL Server 2008管理报表 2.2 规划SQL Server 2008的部署 2.2.1 建立服务器的性能系统 2.2.2 配置I/O子系统 2.2.3 确保可用性和可伸缩性 2.2.4 确保连接...
2.1.3 使用SQL Server 2008的多维数据库和数据挖掘 2.1.4 使用SQL Server 2008管理报表 2.2 规划SQL Server 2008的部署 2.2.1 建立服务器的性能系统 2.2.2 配置I/O子系统 2.2.3 确保可用性和可伸缩性 2.2.4 确保连接...
2.1.3 使用SQL Server 2008的多维数据库和数据挖掘 2.1.4 使用SQL Server 2008管理报表 2.2 规划SQL Server 2008的部署 2.2.1 建立服务器的性能系统 2.2.2 配置I/O子系统 2.2.3 确保可用性和可伸缩性 2.2.4 确保连接...