首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-09 
如何判断一个数据是否存在参数@namevarchar(50)插入数据时用存储过程,先判断表中是否已存在这个数据(函数

如何判断一个数据是否存在
参数@name   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 字段=@name)
set @i=1
return @i
end
[解决办法]
if not exists(select 1 from ta where name=@name)
insert ta values(@name)
else
print '存在了 '
[解决办法]
create proc sp_check
(
@name varchar(50)
)
if not exists (select * from 表 where username=@name)
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 '实际数据 '

热点排行