將表中的資料生成INSERT語句

2021-05-22 00:22:34 字數 1670 閱讀 5368

create procedure   [dbo].[outputdata]  

@tablename varchar(100)--表名

asdeclare @isidentity int

declare @columnname varchar(100)--列名

declare @typename varchar(100)--資料型別

declare @columns varchar(8000)--

declare @columnsandhead varchar(8000)--

set @columnsandhead = 'insert into '+@tablename+'(['

set @columns = ''

--獲取表的所有欄位名稱

declare a cursor for

select columnproperty( a.id,a.name,'isidentity') as isidentity, a.[name] as columnname ,b.[name] as typename

from syscolumns a inner join systypes b on a.xtype=b.xtype and b.xtype=b.xusertype

where a.[id]=(select [id] from sysobjects where [name]=@tablename)

open a

fetch next from a into @isidentity, @columnname ,@typename

while @@fetch_status = 0

begin

if @isidentity =0

begin

if @typename in ('bigint','bit','decimal','float','int','money','numeric','real','smallint','smallmoney','tinyint')

begin

set @columns = @columns + 'isnull(cast(['+@columnname +'] as varchar),''null'')+'',''+'

endelse

begin

set @columns = @columns+ '''''''''+ isnull(cast(['+@columnname +'] as varchar(50)),''null'')+ ''''''''+'',''+'

endset @columnsandhead = @columnsandhead + ''+ @columnname +'],['

endfetch next from a into @isidentity, @columnname ,@typename

endselect @columnsandhead = left(@columnsandhead,len(@columnsandhead)-2) +') values('''

select @columns = left(@columns,len(@columns)-5)

close a

deallocate a

exec('select '''+@columnsandhead+'+'+@columns +'+'')'' as insertsql from '+@tablename)

將表資料生成Insert插入語句

參考一下這個 將表資料生成sql指令碼的儲存過程 create procedure dbo.uspoutputdata declare tablename sysname select tablename dbo.sys roleinfo 表名 declare column varchar 1000...

將表中資料轉換成Insert語句

object storedprocedure dbo spgeninsertsql script date 02 01 2012 10 48 38 set ansi nulls on goset quoted identifier on goalter procedure dbo spgeninse...

根據表中資料生成insert語句的儲存過程

根據表中資料生成insert語句的儲存過程 alter proc proc insert tablename varchar 256 wherestr nvarchar 1000 as 表名稱 begin set nocount on declare sqlstr varchar 4000 sqls...