生成Insert語句SQL儲存過程

2021-04-19 22:23:03 字數 1687 閱讀 2109

create  procedure dbgendata

@tablename  varchar(100) --表名

asdeclare @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 a.[name] as columnname ,b.[name] as typename

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

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

open a

fetch next from a into @columnname ,@typename

while @@fetch_status = 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(1000)),''null'')+'',''+'

endelse

begin

set @columns = @columns+  ' (case when '+@columnname +' is null then '''' else '''''''' end) + isnull(cast('+@columnname +' as varchar(1000)),''null'')+ (case when '+@columnname +' is null then '''' else '''''''' end) +'',''+'

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

fetch next from a into @columnname ,@typename

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

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

close a

deallocate a  

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

print 'select '''+@columnsandhead+'+'+@columns +'+'')'' as a from '

go

生成insert語句的儲存過程

create procedure dbgendata tablename varchar 100 表名 as declare isidentity int declare columnname varchar 100 列名 declare typename varchar 100 資料型別 decl...

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,...

SQL 十七 INSERT語句

sql表將資料按行儲存,一行接一行。insert into語句用於向資料庫中的表新增新的資料行。sql insert into語法如下 insert into table name values value1,value2,value3,確保值的順序與表中的列的順序相同。考慮以下employees表...