当前位置: 代码迷 >> SQL >> sqlserver导出sql话语的存储过程
  详细解决方案

sqlserver导出sql话语的存储过程

热度:111   发布时间:2016-05-05 14:58:29.0
sqlserver导出sql语句的存储过程
CREATE PROCEDURE dbo.UspOutputData@tablename sysnameASdeclare @column varchar(1000)declare @columndata varchar(1000)declare @sql varchar(4000)declare @xtype tinyintdeclare @name sysnamedeclare @objectId intdeclare @objectname sysnamedeclare @ident intset nocount onset @objectId=object_id(@tablename)if @objectId is null -- 判断对象是否存在beginprint 'The object not exists'returnendset @objectname=rtrim(object_name(@objectId))if @objectname is null or charindex(@objectname,@tablename)=0 --此判断不严密beginprint 'object not in current database'returnendif OBJECTPROPERTY(@objectId,'IsTable') < > 1 -- 判断对象是否是tablebeginprint 'The object is not table'returnendselect @ident=status&0x80 from syscolumns where [email protected] and status&0x80=0x80if @ident is not nullprint 'SET IDENTITY_INSERT [email protected]+' ON'declare syscolumns_cursor cursorfor select c.name,c.xtype from syscolumns c where [email protected] order by c.colidopen syscolumns_cursorset @column=''set @columndata=''fetch next from syscolumns_cursor into @name,@xtypewhile @@fetch_status < >-1beginif @@fetch_status < >-2beginif @xtype not in(189,34,35,99,98) --timestamp不需处理,image,text,ntext,sql_variant 暂时不处理beginset @[email protected]+case when len(@column)=0 then'' else ',[email protected]set @[email protected]+case when len(@columndata)=0 then '' else ','','','end+case when @xtype in(167,175) then [email protected]+'+''''''''' --varchar,charwhen @xtype in(231,239) then [email protected]+'+''''''''' --nvarchar,ncharwhen @xtype=61 then '''''''''+convert(char(23),[email protected]+',121)+''''''''' --datetimewhen @xtype=58 then '''''''''+convert(char(16),[email protected]+',120)+''''''''' --smalldatetimewhen @xtype=36 then '''''''''+convert(char(36),[email protected]+')+''''''''' --uniqueidentifierelse @name endendendfetch next from syscolumns_cursor into @name,@xtypeendclose syscolumns_cursordeallocate syscolumns_cursorset @sql='set nocount on select ''insert [email protected]+'([email protected]+') values(''as ''--'',[email protected]+','')'' from [email protected]print [email protected]exec(@sql)if @ident is not nullprint 'SET IDENTITY_INSERT [email protected]+' OFF'GO

?

?

上面是添加存储过程。

?

执行下面的既可以得到“insert into tablename ……”的语句表,ctrl+a全选复制到文本即可。

?

?

exec UspOutputData tablename
  相关解决方案