How to use the Postgresql pl/sql FOR LOOP statement- 6 Examples including keywords EXIT and CONTINUE

Поділитися
Вставка
  • Опубліковано 24 лип 2024
  • FOR LOOP statements are available in PostgreSQL to iterate over a range of integers, a result set, or the result set of a dynamic query.
    A FOR loop in PostgreSQL is a control structure that allows you to execute a block of code repeatedly for a specific number of times or until a specific condition is met. It is useful for performing a set of operations on a collection of data, such as iterating over a table and performing an operation on each row. For example, you could use a FOR loop to iterate over a table of data and insert each row into another table.
    Table of Contents
    00:00 Hey Team
    00:08 First example - basic FOR statement
    01:05 Example 2 - Fahrenheit to Celsius, using FOR STATEMENT
    02:57 - Example 3 - Use For loop in REVERSE order - show ascii characters
    03:51 - add consecutive numbers, also see Carl Gauss method
    05:54 - How to use the EXIT keyword when using the FOR statement
    07:23 - How to use the CONTINUE keyword when using the FOR statement
    09:01 - End of video message
    how to use the "Raise Notice" method to display output results
    #softwareNuggets, ‪@SoftwareNuggets‬, #share_to_be_better

КОМЕНТАРІ • 14

  • @davidwhite2011
    @davidwhite2011 Рік тому +1

    Thanks!

  • @sanje_23
    @sanje_23 Рік тому

    Hi there! thank you for this concept of the for loop , anyways are there any possible ways to make a loop and if it hits that certain number it will loop again to generate a new set of number?
    for example if I have a set of cards where in for every 10 cards I have to separate it, and then continue on shuffling until there's 10 cards again and then separate it again somewhere like that?
    THANK YOU IN ADVANCE

    • @SoftwareNuggets
      @SoftwareNuggets  Рік тому

      hey @soundofthewaves8824 ,
      a deck of cards has 52 unique cards,
      you have a loop, that picks 10 cards,
      after the 10th card is selected
      you want to have the routine select 10 more cards
      now, the deck has 52 cards, when we remove 10 cards on the first loop, are the first set of cards (the original 10 cards) placed back into the full deck?
      this may take several attempts for me to understand your problem, but we will get a solution.
      --scott

    • @sanje_23
      @sanje_23 Рік тому

      @@SoftwareNuggets
      "the deck has 52 cards, when we remove 10 cards on the first loop, are the first set of cards (the original 10 cards) placed back into the full deck?"
      What my thought here is that will I be able to make the same loop again like in these 52 cards after we remove 10 I would just like to continue on removing 10 until there's 2 remaining I don't know if my question makes sense Im just trying to learn I'm just a newbie😅Thanks for the quick reply tho! really appreciate it!

    • @SoftwareNuggets
      @SoftwareNuggets  Рік тому +1

      hey @soundofthewaves8824 ,
      so, we have 52 cards, we pick 10, and now we have 42, pick 10 again, now we have 32 .... only 2 are left.
      so, the first problem I see, is that; how do we maintain the current state of the available cards.
      are you storing the full deck of cards into a table, a temporary table?
      when we call for the first 10 cards, are they randomly selected, or, when we populate the "table" do you insert the cards in a random order.
      are you writing a function to get the "current 10" cards?
      when the deck gets down to 2 cards, do you just truncate the table, regenerate the random order, insert, then start displaying the next set of 10 cards?
      --- ideas
      you have a table that contains 52 cards
      table has index, and card face
      1 = Ace Hearts
      2 = 2 Hearts
      ....
      select trunc(random()*52)+1
      this will return you a random index value for 52 possible entries
      after you pick one card, there are only 51 possible entries, so we have to modify
      select trunc(random()*51)+1
      when the count(*) of the table = 2, well we have to start the process all over.
      hope this helps

    • @sanje_23
      @sanje_23 Рік тому

      @@SoftwareNuggets thank you so much for this! Also are they any ways like email where I can message you for guidance at some point? 😅

    • @SoftwareNuggets
      @SoftwareNuggets  Рік тому +1

      I created these functions that might help you.
      github.com/softwareNuggets/PostgreSQL_Deck_of_cards_show_10
      let me know what you think.
      i will make a video about loading cards, and selecting random cards, maybe this weekend.
      scott

  • @atulkprajapati
    @atulkprajapati Рік тому

    We are required to support fetching of rows from start to end from a result set in PL SQL and are not required to go to the previous row in the result set. Which among the following PL SQL features can we use for the same? External | Implicit | Forward | None of the Cursors
    Could you please explain this?

    • @SoftwareNuggets
      @SoftwareNuggets  Рік тому

      Hey @atulkprajapati,
      did you view my cursor video : ua-cam.com/video/o4blx6Ij5S8/v-deo.html
      I also found this article on the internet, that is a good read. www.databasestar.com/sql-cursor/
      I think, after you watch my video, you should understand how CURSORS work in PostgreSQL.
      I am unaware of External or Implicit cursor types. If you have a document that talks about these terms, can you send to me?
      Scott

    • @atulkprajapati
      @atulkprajapati Рік тому

      @@SoftwareNuggets yes I did...
      But did not understand

    • @SoftwareNuggets
      @SoftwareNuggets  Рік тому

      Okay, I’m going to help you learn cursors. Let’s use this email address: softwareNugget65@gmail.com
      Send me an email so we can start