SQL Server 儲存過程生成insert語句

2021-09-09 04:14:49 字數 4234 閱讀 3437

原文:

sql server 儲存過程生成insert語句

你肯定有過這樣的煩惱,同樣的表,不同的資料庫,加入你不能執行select  insert

那麼你肯定需要一條這樣的儲存過程,之需要傳入表明,就會給你生成資料的插入語句。

當然資料表數量太大,你將最好用別的方式

create

proc

[dbo

].[spgeninsertsql

] (@tablename

varchar(256

))as

begin

declare

@sql

varchar(8000

)declare

@sqlvalues

varchar(8000

)set

@sql='

('set@sqlvalues='

values (''+

'select

@sqlvalues

=@sqlvalues

+ cols +'+

'',''+

' ,@sql

=@sql+'

['+ name +'],

'from

(select

case

when xtype in (48,52,56,59,60,62,104,106,108,122,127

)

then

'case when

'+ name +

'is null then

''null

''else '+

'cast(

'+ name +

'as varchar)'+

'end

'when xtype in (58,61

)

--then '''''''''+convert(char(23),'+name+',121)+''''''''' --datetime

then

'case when

'+ name +

'is null then

''null

''else '+

'''''''''+ '

+'cast(

'+ name +

'as varchar)'+

'+'''''''''+'

end'

when xtype in (167

)

then

'case when

'+ name +

'is null then

''null

''else '+

'''''''''+ '

+'replace(

'+ name+',

''''''''

,'''''''''''')'

+'+'''''''''+'

end'

when xtype in (231

)

then

'case when

'+ name +

'is null then

''null

''else '+

'''n

''''''+ '

+'replace(

'+ name+',

''''''''

,'''''''''''')'

+'+'''''''''+'

end'

when xtype in (175

)

then

'case when

'+ name +

'is null then

''null

''else '+

'''''''''+ '

+'cast(replace(

'+ name+',

''''''''

,''''''''''''

) as char('+

cast(length as

varchar) +

'))+

'''''''''+'

end'

when xtype in (239

)

then

'case when

'+ name +

'is null then

''null

''else '+

'''n

''''''+ '

+'cast(replace(

'+ name+',

''''''''

,''''''''''''

) as char('+

cast(length as

varchar) +

'))+

'''''''''+'

end'

else

'''null

'''end

ascols,name

from

syscolumns

where id =

object_id(@tablename

) ) t

set@sql='

select

''insert into ['+

@tablename+'

]'+left(@sql,len(@sql)-

1)+') '

+left(@sqlvalues,len(@sqlvalues)-

4) +')

''from '+

@tablename

print

@sql

exec (@sql

)end

sql語句

最後的結果:

insert into [syssample] ([id],[name],[age],[bir],[photo],[note],[createtime]) values ('0002ca83-af2f-4d8f-a345-33ca1cc7cf3c','任務排程系統',18,'2013-01-02 21:42:30.013','',null,'2013-01-02 21:42:30.013')

insert into [syssample] ([id],[name],[age],[bir],[photo],[note],[createtime]) values ('0004a6f3-ec28-4d1f-ba40-0fc4b2218c92','任務排程系統',18,'2013-07-09 19:36:00.060','',null,'2013-07-09 19:36:00.060')

insert into [syssample] ([id],[name],[age],[bir],[photo],[note],[createtime]) values ('00094d35-7b51-4ea3-871e-ce17e293b157','任務排程系統',18,'2013-05-16 15:21:20.070','',null,'2013-05-16 15:21:20.070')

insert into [syssample] ([id],[name],[age],[bir],[photo],[note],[createtime]) values ('000bfbb0-b37d-4d6e-9fa2-3069d4f18f84','任務排程系統',18,'2013-04-11 11:41:50.030','',null,'2013-04-11 11:41:50.030')

insert into [syssample] ([id],[name],[age],[bir],[photo],[note],[createtime]) values ('000c2cbc-e358-4469-bc2c-04f4ddcd72cd','任務排程系統',18,'2013-05-06 16:07:00.037','',null,'2013-05-06 16:07:00.037')

insert into [syssample] ([id],[name],[age],[bir],[photo],[note],[createtime]) values ('000cb795-40ec-4783-b7a4-8d298df63b70','任務排程系統',18,'2013-01-23 20:52:30.030','',null,'2013-01-23 20:52:30.030')

SQL Server 儲存過程生成insert語句

你肯定有過這樣的煩惱,同樣的表,不同的資料庫,加入你不能執行select insert 那麼你肯定需要一條這樣的儲存過程,之需要傳入表明,就會給你生成資料的插入語句。當然資料表數量太大,你將最好用別的方式 create proc dbo spgeninsertsql tablename varcha...

Sqlserver儲存過程生成日期維度

話不多說,之前已經有一篇日誌是利用oracle的儲存過程生成日期維度表,接下來我們就用sqlserver來實現這個操作,如下面的步驟所示 1 建立日期維度表 dim time use dw go object table dbo dim time script date 12 19 2015 15 ...

sql server儲存過程

建立表的語句 create table student sno int primary key,sname nvarchar 30 sgentle nvarchar 2 sage int,sbirth smalldatetime,sdept nvarchar 30 drop table studen...