SQL Server中根据条件生成Insert语句

 

select
'insert into 表名 values('
+ case when 字段1 is null then 'null' else ''''+CONVERT(varchar(8000),字段1)+'''' end
+ ',' +case when 字段2 is null then 'null' else '''' + CONVERT(varchar(8000),字段2) + '''' end
+ ',' +case when 字段3 is null then 'null' else '''' + CONVERT(varchar(8000),字段3) + '''' end
+ ',' +case when 字段4 is null then 'null' else '''' + CONVERT(varchar(8000),字段4) + '''' end
+ ',' +case when 字段5 is null then 'null' else '''' + CONVERT(varchar(8000),字段5) + '''' end
+')'
from 表名

where  UserID = 'xxx'

 

posted @ 2022-04-20 11:12  阿日斯兰  阅读(381)  评论(0编辑  收藏  举报