insert table1(id1 = substring(table.id,1,6),name1=table.name,re1=table.re)
select table(id,name,re) form table where table.id not exists
select table1.id form table where table1.id = substring(table.id,1,6)怎么不对呀
select table(id,name,re) form table where table.id not exists
select table1.id form table where table1.id = substring(table.id,1,6)怎么不对呀
但我的语句好象不对请问怎么改
insert into table1(id1,name1,re1) select substring(id,1,6),name,re from table
where id='xxxxxxx'
这样。
如果id1有唯一索引,那么这样就可以了,不会重复插入。如果不唯一,可以用大概如下形式:
insert into table1(id1,name1,re1) select substring(id,1,6),name,re from table
where id='xxxxxxx' and not exist
(select * from table1 where id1=substring('xxxxxxxx',1,6) )不太了解sql server,不知道substring函数是否正确,
另,不知是否有其它更简单形式代替上面的子查询
可能是一条
也可能是n条是对整两个数据库进行操作
有点类似于批处理
多个就用 in ( )
整个表就把条件给去了meteorlet说得对,是要类型转换得
declare @t2 table(id varchar(6),name varchar(50),re varchar(20))--insert t1
insert @t1
select '123456789','aaa',''
union all
select '123676789','aaa',''
union all
select '123676783','aaa',''select * from @t1insert @t2 select substring(t.id,1,6),name,re from @t1 t where not exists(select 1 from @t2 where id=substring(t.id,1,6))select * from @t2