日期:2014-05-17  浏览次数:20467 次

生成插入语句的存过有点问题
语句如下:
--1. 选中你要操作的数据库, 执行以下sql语句

IF OBJECT_ID('spGenInsertSQL','P') IS NOT NULL 
DROP PROC spGenInsertSQL
GO
CREATE proc 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 '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 as Cols,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
GO

 

-- 2. 执行成功以后,调用这个存储过程,要传一个参数: 表名
  exec spGenInsertSQL '表名'
--- 执行后,生成这个表的插入语句,


生成其他表的插入记录时是没问题的。
但是,当生成表yy_sg.CS_TOBACCO_BIZ_AUDIT的插入记录时报错如下:

select 'INSERT INTO [yy_sg.CS_TOBACCO_BIZ_AUDIT] ([TOBACCO_BIZ_AUDIT_ID],[INVOICE_NO],[PERSON_NO],[PERSON_NAME],[WEIGHT],[AMOUNT],[ALLOWANCE_AMOUNT1],[ALLOWANCE_AMOUNT2],[DEDUCT_AMOUNT],[TRANSFER_AMOUNT],[TRANSFER_STYLE],[TRANSFER_STATUS],[AUDIT_IS_SUCC],[CHECK_RANK],[IS_FINAL_AUDIT],[AUDIT_OPERATOR_UUID],[AUDIT_OPERATOR_NAME],[INVOICING_STYLE],[INVOICING_DATE],[BUSINESS_YEAR],[ORG_CD],[SEND_STATE],[MODIFY_TIME],[SEND_TIME],[PACKAGE_TIME],[DATA_STATE],[LAST_TIME],[RESERVED],[RESERVED3],[TRANSFER_DATE],[CHECK_AMOUNT_STATUS],[CHECK_AMOUNT_INFO_CD],[TRANSFER_INFO_CD],[BANK_TRANSFER_DATE],[VERSION],[REVERSAL_STATUS],[REVERSAL_INVOICE_NO],[JHLX],[INVOICE_PRINT_STATE]) values ('+case when TOBACCO_BIZ_AUDIT_ID is null then 'NULL' else '''' + cast(replace(TOBACCO_BIZ_AUDIT_ID,'''','''''') as Char(32))+'''' end + ',' + case when INVOICE_NO is null then 'NULL' else '''' + replace(INVOICE_NO,'''','''''')+'''' end + ',' + case when PERSON_NO is null then 'NULL' else '''' + replace(PERSON_NO,'''','''''')+'''' end + ',' + case when PERSON_NAME is null then 'NULL' else '''' + replace(PERSON_NAME,'''','''''')+'''' end + ',' + case when WEIGHT is null then 'NULL' else cast(WEIGHT as varchar) end + ',' + case when AMOUNT is null then 'NULL' else cast(AMOUNT as varchar) end + ',' + case when ALLOWANCE_AMOUNT1 is null then 'NULL' else cast(ALLOWANCE_AMOUNT1 as varchar) end + ',' + case when ALLOWANCE_AMOUNT2 is null then 'NULL' else cast(ALLOWANCE_AMOUNT2 as varchar) end + ',' + case when DEDUCT_AMOUN