博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
把SQL数据库生成脚本插入的 SQL语句
阅读量:7237 次
发布时间:2019-06-29

本文共 2241 字,大约阅读时间需要 7 分钟。

--将表数据生成SQL脚本的存储过程

CREATE PROCEDURE dbo.UspOutputData
@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 'The object not exists'
return
end
set @objectname=rtrim(object_name(@objectId))
if @objectname is null or charindex(@objectname,@tablename)=0 --此判断不严密
begin
print 'object not in current database'
return
end
if OBJECTPROPERTY(@objectId,'IsTable') < > 1 -- 判断对象是否是table
begin
print 'The object is not table'
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'
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'
GO
exec UspOutputData 你的表名

转载于:https://www.cnblogs.com/yc1990/archive/2013/04/12/3017011.html

你可能感兴趣的文章
两个与后台有关的回调处理
查看>>
idhttp.post方式 调用datasnap rest 远程方法
查看>>
Gulp快速入门
查看>>
TClientDataSet的 fastscript封装
查看>>
有用的国外开源项目网址
查看>>
DataGridView 绑定DataTable方式编辑保存的bug?
查看>>
ComboBox 使用数据绑定时 Sorted 属性的bug
查看>>
BZOJ 3172 单词(ac自动机)
查看>>
具体数学第二版第四章习题(2)
查看>>
DotNetBar.7.0 Crack
查看>>
D3D中深度测试和Alpha混合的关系
查看>>
延时执行和取消延时执行
查看>>
关于线程安全
查看>>
使用Java自带的VisualVM监控远程主机JVM内存使用情况
查看>>
123——Appium Girls活动
查看>>
Linux系统CPU频率调整工具使用
查看>>
使用大于16TB的ext4文件系统
查看>>
jquery ajax cache的问题
查看>>
VIM 与 系统剪切版
查看>>
解决刷新界面滚动条位置还原的问题
查看>>