我想检查列名中的条目是否存在。
但每次我尝试使用“if”或“if exists”时它会给我一个语法错误。
(从class101中选择1 WHERE name ='Peter')
工作并返回“我在一排”
(从class101 WHERE name ='Peter123'中选择1)
工作并返回我“空集”
现在我想插入一些不存在的东西。
if not exists (select 1 from class101 where name = 'Peter123')
BEGIN
insert into class101 values ('Peter123')
END;
这给了我一个语法错误
我希望有人可以帮我找到错误。
SELECT COUNT(*)FROM class101 WHERE name =“Peter123”;
这将返回存在的Peter123的数量。
列名必须括在括号中,因为它是sql中的保留字,所以请尝试:
IF NOT EXISTS (select 1 from class101 where [name] = 'Peter123')
BEGIN
INSERT INTO class101 VALUES ('Peter123')
END;
您对该表的该列具有唯一键约束。这样只有一个这样的值可以插入到该列中,否则如果你想通过sql single命令解决,
insert into class101 (select '''PETER''' from dual where not exists (select 1 from class101 where name like 'PETER'));
否则通过plsql:
declare
l_flag varchar2(2) := 'N';
begin
select 'Y' into l_flag from class101 where name='PETER' and rownum=1;
if l_flag = 'Y'
then
insert into class101 values ('PETER');
end if;
exception
when no_data_found
then
null;
end;