--主要是想检查,学生表里是否有数据,如果有显示,没有的话加一条,但是我这里语句报错,求大家帮看看
--我用的pl/sql,如果直接执行下面的会报错,是我需要设置什么还是我写的有错呢?declare 
int_Count number(10);begin
select count(pkid) into int_Count from students; 
if int_Count>0 then
 select * from  students
else
 INSERT INTO students VALUES ('小明')
end if;
end begin

解决方案 »

  1.   

    declare 
    int_Count number(10);begin
    select count(pkid) into int_Count from students; 
     select * from  students(不过这里如果要返回一个数据集,不能这么写,呵呵,懒得弄了,默认楼主是为了简化才这么表示)
         EXCEPTION
    WHEN NO_DATA_FOUND THEN
    INSERT INTO students VALUES ('小明');
    end begin
      

  2.   

    declare
    int_Count number(10);
    cursor cur;
    fname varchar2(100);
    begin
    select count(pkid) into int_Count from students;
    if int_Count>0 then
     open cur for select name from students;
     loop
       fetch cur into fname;
       exit when cur%notfound;
       dbms_output.put_line( fname );
     end loop;
     close cur;
    else
     INSERT INTO students VALUES ('小明')
    end if;
    end;
      

  3.   

    >>select count(pkid) into int_Count from students;
    不会引发异常,当没有记录的时候count(pkid) 为0
      

  4.   

    hongqi162(失踪的月亮) 正解
      

  5.   

    求助:我这里还是不行,就只写这两句都报错?? 是不是我的pl/sql汉化过的原因啊? 还是需要设置什么??
    -------------------------------------------------------------------------declare int_Count number(10);
    select count(pkid) into int_Count from students;
      

  6.   

    declare 
    int_Count number(10);begin
    select count(pkid) into int_Count from students; 
    if int_Count>0 then
     select * from  students
    else
     INSERT INTO students VALUES ('小明')
    end if;
    end begin
    if(exists(select pkid into int_Count from students; )) then
    select * from students
    else
     INSERT INTO students VALUES ('小明')
    end if;
      

  7.   

    用了select 就要用into将变量值放在数据库里
      

  8.   

    select * from students; pl/sql中不能返回数据集;
    若数据表中不只一个字段,则应指明需要插入数据的字段