生成insert語句的儲存過程

2021-04-18 11:26:49 字數 1677 閱讀 3302

create procedure dbgendata

@tablename varchar(100)--表名

as declare @isidentity int

declare @columnname varchar(100)--列名

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

declare @columns varchar(3000)--

declare @columnsandhead varchar(3000)--

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','**allint','**allmoney','tinyint')

begin

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

endelse

begin

set @columns = @columns+ '''''''''+ isnull(cast(['+@columnname +'] as varchar),''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)

go

生成Insert語句SQL儲存過程

create procedure dbgendata tablename varchar 100 表名 asdeclare columnname varchar 100 列名 declare typename varchar 100 資料型別 declare columns varchar 8000...

excel生成insert語句

在需要生成sql的資料 後面輸入如下語句 concatenate insert into bts business2.b bts hive plan target date,plan id,version id,target name,target value values a2,b2,c2,d2,...

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

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