如何在plsql中使用带if的select
我正试图打印一条消息,告诉您用户是否退出employee表 这是我的密码如何在plsql中使用带if的select,plsql,Plsql,我正试图打印一条消息,告诉您用户是否退出employee表 这是我的密码 DECLARE empid employees.employee_id%type := &id; BEGIN if empid IS NOT NULL Then DBMS_OUTPUT.PUT_LINE('user Exists'); elsif if empid IS NULL Then DBMS_OUTPUT.PUT_LINE('User doesnt exist'); END; / 它返回用户出口,即
DECLARE
empid employees.employee_id%type := &id;
BEGIN
if empid IS NOT NULL Then
DBMS_OUTPUT.PUT_LINE('user Exists');
elsif
if empid IS NULL Then
DBMS_OUTPUT.PUT_LINE('User doesnt exist');
END;
/
它返回用户出口,即使它没有我使用数据测试它,我不知道在哪里更改,或者我是否使用select语句来选择数据
任何帮助都将不胜感激您需要从select中填充变量。通常,它的工作原理如下: 从emp中选择id进入empid,其中id=1234 但是要小心。如果ID不存在,那么您将获得ora-01403。要使用此方法将其选择到变量中,结果集中需要有一条记录。因此,如果您的ID不存在,请按如下方式重新排列查询以返回null: 从emp中选择id,其中id=1234,然后从dual中选择empid2种可能的解决方案:
DECLARE
empid employees.employee_id%type := &id;
cnt integer;
BEGIN
select count(*) into cnt from emp where id = empid;
if cnt > 0 Then
DBMS_OUTPUT.PUT_LINE('user Exists');
else
DBMS_OUTPUT.PUT_LINE('User doesnt exist');
end if;
END;
/
或者,如果您想获得有关该员工的一些信息:
DECLARE
empid employees.employee_id%type := &id;
empname employees.employee_name%type;
BEGIN
select name into empname from employees where employee_id = empid;
DBMS_OUTPUT.PUT_LINE('user Exists name='||empname);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('User doesnt exist');
END;
/
是的,您需要使用select语句来选择数据。您目前所做的只是确认用户已为&id输入了一些内容。NVL中没有任何点?只需从emp中选择select id,其中id=1234,然后从dual中选择empid。谢谢,如果用户存在,我实现了select nd i cn显示。我的问题是不存在的用户。我必须使用选择和if,因为我不想要1或其他打印的nt。对不起,托马斯,我的意思是我必须使用2个选择,因为我想显示用户(如果存在),如果用户不存在,则显示消息。