我在网上查看过这么多的教程,我看到了很多类似的问题,因为他们使用不同的SQL实现,所以不回答我的问题。我正在使用SQLPlus,oracle 10g。
CREATE OR REPLACE PROCEDURE getuserid
(uname in varchar) AS
BEGIN
select accountId from "ValidClients"
where username = uname
END getuserid;
代码'SELECT accountId from“ValidClients”WHERE username ='testname'; '工作正常,所以我无法理解我做错了什么让SQLPlus给我一个编译错误。
编辑:没有INTO条款就是答案,感谢所有帮助过的人。一方面问题解决了,我可以要求确认:java程序将调用这些存储过程并尝试将它们存储在结果集中。以下代码是否适用于上述存储过程?CallableStatement cs = connection.prepareCall("{call getuserid(?)}");
cs.setString(1, user);
ResultSet rs = cs.executeQuery();
int userid = Integer.parseInt(rs.getString(1));
EDIT2:忽略上面的内容,它是为子孙后代保留的。它不仅足以满足自己的问题,而且可以通过Google搜索轻松解决,而且不需要定制的答案。
答案 0 :(得分:3)
您在哪里放置查询结果?假设accountId是一个数字,您可以尝试:
CREATE OR REPLACE PROCEDURE getuserid
(uname_p in varchar, accountId_p out NUMBER) AS
BEGIN
select accountId into accountId_p from "ValidClients"
where username = uname_p;
END getuserid;
此致
答案 1 :(得分:1)
在选择的末尾添加分号,您需要select into
变量
e.g:
CREATE OR REPLACE PROCEDURE getuserid (uname in varchar) AS
v_account Number;
BEGIN
SELECT accountId INTO v_account
FROM "ValidClients"
WHERE username = uname;
END getuserid;
答案 2 :(得分:1)
应该是:
CREATE OR REPLACE
PROCEDURE getuserid (
uname in varchar2
)
AS
v_account_id ValidClients.account_id%TYPE;
BEGIN
select accountId
into v_account_id
from "ValidClients"
where username = uname;
END getuserid;
您可能想要考虑将要返回的值。 要么输出OUT参数,要么更好,如果要返回一个值,请将其设为FUNCTION。
CREATE OR REPLACE
FUNCTION getuserid (
uname in varchar2
)
RETURN ValidClients.account_id%TYPE
AS
v_account_id ValidClients.account_id%TYPE;
BEGIN
select accountId
into v_account_id
from "ValidClients"
where username = uname;
RETURN v_account_id;
EXCEPTION
WHEN no_data_found
THEN
-- Handle account ID not being present
WHEN others
THEN
-- Handle other errors.
END getuserid;