当前位置: 代码迷 >> Sql Server >> 怎么判断一个数据是否存在
  详细解决方案

怎么判断一个数据是否存在

热度:71   发布时间:2016-04-27 21:02:25.0
如何判断一个数据是否存在
[email protected]   varchar(50)
插入数据时用存储过程,先判断表中是否已存在这个数据(函数或存储过程实现);若已存在,不操作;不存在,插入数据
没有设置唯一性约束

求SQL语句,谢谢~~

------解决方案--------------------
表结构不清,建议一语句:
Insert table TA(ID,....)
select @ID,.... where @ID not in (Select ID from TA)
------解决方案--------------------
create function f_ifexists(@name varchar(50))
returns int
as
begin
declare @i int
set @i=0
if exists(select 1 from 表 where [email protected])
set @i=1
return @i
end
------解决方案--------------------
if not exists(select 1 from ta where [email protected])
insert ta values(@name)
else
print '存在了 '
------解决方案--------------------
create proc sp_check
(
@name varchar(50)
)
if not exists (select * from 表 where [email protected])
insert into ..

go
------解决方案--------------------
create proc prExistData(@name varchar(50))
as
begin
if exists(select * from test where a = @name)
print 'Data Exist! '
else
insert test(列名) select @name
end
--测试
exec prExistData '实际数据 '
  相关解决方案