国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

根據(jù)基本表結(jié)構(gòu)及其數(shù)據(jù)生成 insert ... 的 SQL

2024-07-21 02:09:10
字體:
供稿:網(wǎng)友

create  proc spgeninsertsql
@tablename as varchar(100)
as
--declare @tablename varchar(100)
--set @tablename = 'orders'
--set @tablename = 'eeducation'
declare xcursor cursor for
select name,xusertype
from syscolumns
where (id = object_id(@tablename))
declare @f1 varchar(100)
declare @f2 integer
declare @sql varchar(8000)
set @sql ='select ''insert into ' + @tablename + ' values('''
open xcursor
fetch xcursor into @f1,@f2
while @@fetch_status = 0
begin
    set @sql [email protected] +
              + case when @f2 in (35,58,99,167,175,231,239,61) then ' + case when ' + @f1 + ' is null then '''' else '''''''' end + '  else '+' end
              + 'replace(isnull(cast(' + @f1 + ' as varchar),''null''),'''''''','''''''''''')'
              + case when @f2 in (35,58,99,167,175,231,239,61) then ' + case when ' + @f1 + ' is null then '''' else '''''''' end + '  else '+' end
              + char(13) + ''','''
    fetch next from xcursor into @f1,@f2
end
close xcursor
deallocate xcursor
set @sql = left(@sql,len(@sql) - 5) + ' + '')'' from ' + @tablename
print @sql
exec (@sql)

第二版:2003.03.08

alter 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 '[email protected]
  --print @sql
  exec (@sql)
end

第三版: 2003.3.9

alter   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,175)
                       then 'case when '+ name +' is null then ''null'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+''''''''' + ' end'
                  when xtype in (231,239)
                       then 'case when '+ name +' is null then ''null'' else '+'''n'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+''''''''' + ' end'
                  else '''null'''
                end as cols,name
           from syscolumns 
          where id = object_id(@tablename) and autoval is null
        ) t
  set @sql ='select ''insert into ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlvalues,len(@sqlvalues)-4) + ')'' from '[email protected]
  print @sql
  exec (@sql)
/*
select *
from syscolumns 
where id = object_id('test') and autoval is null
*/
end

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 万载县| 合阳县| 池州市| 石柱| 宝兴县| 即墨市| 和静县| 巢湖市| 和林格尔县| 灌云县| 桂平市| 肥西县| 张家港市| 民县| 连平县| 石狮市| 额济纳旗| 资源县| 民丰县| 册亨县| 洛阳市| 札达县| 土默特左旗| 神池县| 温宿县| 时尚| 邓州市| 启东市| 达拉特旗| 开原市| 墨玉县| 华宁县| 泸溪县| 静乐县| 兴山县| 黑河市| 将乐县| 蛟河市| 井陉县| 温宿县| 南澳县|