前两天实施需要将SQL server 中表结构生成插入语句,在网上找到解决方案,可是老出错,整理了一下,转贴在此,供参考吧!
步骤一、在sqlserver中建立存储过程。
CREATE procedure 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 xt