SQLSever 生成导入数据

当需要把数据导入到 带有自增列的主子表的时候,稍显复杂。用以下SQL 实现( ResKey 是主表, ResValue 是子表)

 

--- 有效的 CSM 资源。
select * 
into #t
from dbo.VTxtRes
where [Key]  not in ( select [Key] from PM_VTxtRes )
and [Key] not like  '%.0' 
and  [Key] not like 'MyOql.ProductTypeEnum.%'
and [Key] != 'MyOql.EmptyEnum.选择类型'
and [Value] is not null
order by Value
 
 
select 
'insert into ResKey ( [Key]  ) values ( ''' + [Key] + ''');  insert into ResValue (Lang,ResID,[Value]) values ( ' + cast( Lang as varchar(20))  + ' ,SCOPE_IDENTITY(), ''' + [Value] + ''' ) ;'
from #t

 

将会生成:

insert into Res_Key ( [Key] ) values ( 'MyOql.TaskVistLevelEnum.Level1'); insert into Res_Value (Lang,ResID,[Value]) values ( 2 ,SCOPE_IDENTITY(), '1' ) ;

此方法具有一定通用性,作为记录。

posted @ 2012-09-07 16:42  NewSea  阅读(231)  评论(0编辑  收藏  举报