Oracle 存储过程

发布于 2024-08-17 09:15:43 字数 287 浏览 12 评论 0 原文

如何使用oracle xe制作oracle程序,如何检查输入是否有效?例如:如果我的输入是数字,并且我输入 char,那么该过程会打印出一些内容,在这种情况下,我已经处理过 SQL,但没有处理过此类过程?感谢任何帮助

更新

这是一个虚拟示例..我的意思是从最简单的事情开始,然后继续进行更复杂的示例,我真正需要的是检查table books 中的 >field book 等于 0,则停止借用查询 进行插入,否则插入。

how can I make oracle procedure with oracle xe, how can I check if input is valid or not? ex: if my input is number and I type in char that procedure prints out something in that case, I've dealt with SQL but not with these kind of procedures? any help is appreciated

UPDATE

This was a dummy example .. what I meant is to start from the most simple thing then move on up to the more complicated examples, what I actually need is check if a field book in table books equals 0 then stop borrowing query to insert else insert.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少跟Wǒ拽 2024-08-24 09:15:43

这是我认为您想要的那种过程的示例。我不得不做出一些(我希望是有根据的)猜测来填补你的例子中的空白。

create or replace procedure borrow_book
    ( p_book in books.id%type
      , p_borrower in library_member.id%type ) 
as
    cursor cur_book is
        select out_flag
        from books
        where id = p_book
        for update of out_flag;
    rec_book cur_book%rowtype;
begin
    open cur_book;
    fetch cur_book into rec_book;

    if rec_book.out_flag = 0
    then
        raise_application_error(-20000, 'Book is already out on loan.');
    else    
        insert into loans (book_id, borrower_id, issue_date)
        values (p_book, p_borrower, sysdate);
        update books
        set out_flag = 0
        where current of cur_books;
    end if;

    close cur_book;
end borrow_book;
/   

Here is an example of the sort of process I think you want. I have had to make a couple of (I hope educated) guesses to fill in the gaps in your example.

create or replace procedure borrow_book
    ( p_book in books.id%type
      , p_borrower in library_member.id%type ) 
as
    cursor cur_book is
        select out_flag
        from books
        where id = p_book
        for update of out_flag;
    rec_book cur_book%rowtype;
begin
    open cur_book;
    fetch cur_book into rec_book;

    if rec_book.out_flag = 0
    then
        raise_application_error(-20000, 'Book is already out on loan.');
    else    
        insert into loans (book_id, borrower_id, issue_date)
        values (p_book, p_borrower, sysdate);
        update books
        set out_flag = 0
        where current of cur_books;
    end if;

    close cur_book;
end borrow_book;
/   
○闲身 2024-08-24 09:15:43

您的问题听起来不像您需要 PL/SQL。

单个 SQL 插入应该可以(如果我正确理解你的问题):

INSERT INTO new_table
SELECT id, val FROM books WHERE book = 0;

如果你仍然需要一个过程,请将其放入一个过程中:

CREATE OR REPLACE PROCEDURE my_proc AS
BEGIN
  INSERT INTO new_table
  SELECT id, val FROM books WHERE book = 0;
END my_proc;

尝试避免在 PL/SQL 中循环游标并插入值,当它可以在单个 SQL。

Your problem does not sound as if you would need PL/SQL.

A single SQL-insert should do (if I understand your question right):

INSERT INTO new_table
SELECT id, val FROM books WHERE book = 0;

If you still need a procedure, put that into a procedure:

CREATE OR REPLACE PROCEDURE my_proc AS
BEGIN
  INSERT INTO new_table
  SELECT id, val FROM books WHERE book = 0;
END my_proc;

Try to avoid looping over a cursor in PL/SQL and inserting values, when it could be done in a single SQL.

水中月 2024-08-24 09:15:43

您的存储过程参数已经是强类型的。如果你有一个“int”参数,有人输入“ABC”作为值,Oracle 会完成它。你不必/需要。

You stored procedures parameters are already strongly typed. If you have an "int" parameter someone types in "ABC" for the value, Oracle will through it out. You won't have to/need to.

那些过往 2024-08-24 09:15:43

像这样的东西吗?

create or replace PROCEDURE BOOK() AS
BEGIN
    declare cursor cur_b is 
        select * from books;
    BEGIN
        FOR book_row IN cur_b LOOP
            IF book_row.book=0 THEN
                INSERT INTO ...
            END IF;
        end loop;
    end;
END BOOK;

Something like this?

create or replace PROCEDURE BOOK() AS
BEGIN
    declare cursor cur_b is 
        select * from books;
    BEGIN
        FOR book_row IN cur_b LOOP
            IF book_row.book=0 THEN
                INSERT INTO ...
            END IF;
        end loop;
    end;
END BOOK;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文