Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 498281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:51:49+00:00 2026-05-13T05:51:49+00:00

I’m trying to randomly select a card from a table of cards with columns

  • 0

I’m trying to randomly select a card from a table of cards with columns c_value and c_suit using a procedure. After selecting it, the procedure should update that entry’s taken field to be ‘Y’.

create or replace procedure j_prc_sel_card(p_value OUT number,
                                           p_suit OUT number)
AS

   CURSOR CUR_GET_RAND_CARD IS SELECT c_value, 
                                      c_suit
                                 FROM (SELECT c_value, 
                                              c_suit, 
                                              taken
                                         FROM jackson_card
                                     ORDER BY dbms_random.value)
                                WHERE rownum = 1
                        FOR UPDATE OF taken;

BEGIN

  OPEN CUR_GET_RAND_CARD;
  FETCH CUR_GET_RAND_CARD into p_value, p_suit;

  UPDATE jackson_card 
     SET taken = 'Y' 
   WHERE c_value = p_value 
     AND c_suit = p_suit;

  CLOSE CUR_GET_RAND_CARD;

END;

Then I am trying to get the selected card and output what it is as a start. With this:

SET serveroutput on;

DECLARE v_value number;
        v_suit number;

BEGIN

  j_prc_sel_card(p_value => v_value,p_suit => v_suit);
  DBMS_OUTPUT.PUT_LINE(v_value);
  DBMS_OUTPUT.PUT_LINE(v_suit);

END;
/

However i got the error stated in the title and it seems my way of selecting a random card is stopping me from doing an update. Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:51:49+00:00Added an answer on May 13, 2026 at 5:51 am

    Here is a different take on the scenario (I did also address your immediate problem in a different answer).

    Given that we really are building a card-dealing program (as opposed to working with a test case for a business scenario) I didn’t like the TAKEN column. Updating a table column to mark a transitory state seems wrong. What happens when we want to play another game?

    The following solution resolves this by populating an array with all the cards in a random order upfront (the shuffle). The cards are dealt by simply taking the next entry off the stack. The package offers a choice of approach for running out of cards: either throw a user-defined exception or just cycle through the deck again.

    create or replace package card_deck is
    
        no_more_cards exception;
        pragma exception_init(no_more_cards, -20000);
    
        procedure shuffle;
    
        function deal_one 
            ( p_yn_continuous in varchar2 := 'N')
            return cards%rowtype;
    
    end card_deck;
    /
    
    create or replace package body card_deck is
    
        type deck_t is table of cards%rowtype;
        the_deck deck_t;
    
        card_counter pls_integer;
    
        procedure shuffle is
        begin
            dbms_random.seed (to_number(to_char(sysdate, 'sssss')));
            select *
            bulk collect into the_deck
            from cards
            order by dbms_random.value;
            card_counter := 0;
        end shuffle;
    
        function deal_one
            ( p_yn_continuous in varchar2 := 'N')
            return cards%rowtype
        is
        begin
            card_counter := card_counter + 1;
            if card_counter > the_deck.count() 
            then
                if p_yn_continuous = 'N'
                then
                    raise no_more_cards;
                else
                    card_counter := 1;
                end if;
            end if;
            return the_deck(card_counter);
        end deal_one;
    
    end card_deck;
    /
    

    Here it is in action. Don’t use an open LOOP if you set the continuous dealing mode to Y.

    SQL> set serveroutput on
    SQL>
    SQL> declare
      2      my_card cards%rowtype;
      3  begin
      4      card_deck.shuffle;
      5      loop
      6          my_card := card_deck.deal_one;
      7          dbms_output.put_line ('my card is '||my_card.c_suit||my_card.c_value);
      8      end loop;
      9  exception
     10      when card_deck.no_more_cards then
     11          dbms_output.put_line('no more cards!');
     12  end;
     13  /
    my card is HA
    my card is H7
    my card is DJ
    my card is CQ
    my card is D9
    my card is SK
    no more cards!
    
    PL/SQL procedure successfully completed.
    
    SQL>
    

    You may think I’m not dealing with a full deck. You wouldn’t be the first to think that 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.