SQL 判断重复值
表tb1idfirstNameLastNamesameNmaeFlag100wangping101lilei103lilei103若插入数据时,此记录的firstName...
表tb1
id firstName LastName sameNmaeFlag
100 wang ping
101 li lei
103 li lei 103
若插入数据时,此记录的 firstName 和LastName 已有相同的记录,则在此条记录sameNameFlag字段插入id号,没记录sameNameFlag为空。。
id firstName LastName sameNmaeFlag
100 wang ping
101 li lei
103 li lei 103
如何插入新的记录(SQL) 展开
id firstName LastName sameNmaeFlag
100 wang ping
101 li lei
103 li lei 103
若插入数据时,此记录的 firstName 和LastName 已有相同的记录,则在此条记录sameNameFlag字段插入id号,没记录sameNameFlag为空。。
id firstName LastName sameNmaeFlag
100 wang ping
101 li lei
103 li lei 103
如何插入新的记录(SQL) 展开
展开全部
insert into table1(id,firstname,lastname,samenameflag) select
newid,newfirstname,newlastname,samenameflag from
(select case when count(*)>0 then newid else '' end as samenameflag from table1 where firstname=newfirstname and lastname=newlastname ) tmp
newid等就是你插入的固定值
newid,newfirstname,newlastname,samenameflag from
(select case when count(*)>0 then newid else '' end as samenameflag from table1 where firstname=newfirstname and lastname=newlastname ) tmp
newid等就是你插入的固定值
展开全部
ID是手动插入还是自增的?
做个触发器吧
create tb1_insert
on tb1
after insert
as
begin
declare @id int,@firstName varchar(50),@LastName varchar(50),@qty int
select @id=id,@LastName=LastName,@firstName=firstName from inserted
select @qty=count(*) from tb1 where LastName=@LastName and firstName=@firstName
if @qty>1
update tb1 set sameNmaeFlag=@id where id=@id
end
做个触发器吧
create tb1_insert
on tb1
after insert
as
begin
declare @id int,@firstName varchar(50),@LastName varchar(50),@qty int
select @id=id,@LastName=LastName,@firstName=firstName from inserted
select @qty=count(*) from tb1 where LastName=@LastName and firstName=@firstName
if @qty>1
update tb1 set sameNmaeFlag=@id where id=@id
end
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
触发器写的不错.顶一个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询