hello and welcome to my channel I am Akram and in today's video we will discuss about the cursors available in PostgreSQL there are multiple cursors like simple cursors parameterized cursor for loop cursor and ref cursor so cover all these cursors one by one in different videos so today we will discuss about the simple cursor so let's start with this so before that we need to have one database setup if we don't have database then you can download and install and then load one database sample I'll give the video link regarding that in the video description you can do from there in current for this current video I am using the DVD rental database that I have already loaded here and these are having the data so for example I'll take the actor table so let's check the data from actor table select star from actor If we check this table data there will be many records available. so here are all the records that are available in this actor table so what we'll do we'll use one cursor that will be simple cursor and we'll access one by one record in a loop so for that let's create one stored procedure and we'll use that one to access the data in a loop so first let's start writing say it's like a simple cursor I'm giving the name and then we define the language language is PLPG SQL this is the delimiter one variable will be declaring here suppose a Elvis train horses of character varying let's say size of 200 one variable of record type suppose it's RSU1 of record type variable and one cursor suppose in cursor1 this is for cursor for suppose the select query will be writing it will be select will be taking actor ID first name so basically we'll be showing the actor ID first name and last name of all the actors so for that from actor so here also we can mention the order by clause as well and if any condition where clause and all we can mention here suppose i am doing order by through actor id so that we can verify the data easily so it's all order by actor id so this is my cursor is declared here now let's write the begin part begin of the procedure and this will be the end of the procedure and end of the procedure and this is the delimiter so within begin and end block we will be writing our quotes here suppose the first step is to open the cursor so for that we will be doing open cursor one that cursor we have declared here it is cursor one so I have written here open cursor one and in the loop we will start one loop and in loop what we will do we will do fetch cursor1 into the record1 variable the variable I have declared of record type above okay so one by one it will be very fetching the data in the loop so first step is writing the exit condition here exit when not found when there will be no more values in the cursor so the loop will be exit so here will be accessing those values through one say the variable we have declared suppose a LV string that we have declared will be storing those data here here I am writing actor ID so using the pipeline I receive one dot actor ID since we are using the RSC one as the record so the data that I faced it will be stored in the RS one variable actor ID now there will be there will be other values as well I'm separating commas suppose actor first name so while writing the first name I can mention I receive one dot first name if I'm writing first name because I have given the column name here as first name if I am giving any alias name so that alias name should be used here the this column name that will that will be defined here only these column names are from the cursor variable only these are not the table values okay now next so I have to write actor last name as well I receive one dot last name okay suppose now I'll show you what I'm saying suppose I have given alias name as to suppose say end name okay so here in in this place instead of writing the last name i have to write the end name okay and that will point to the last name that alias we have taken here so the string is prepared here so what we'll do it will fetch one by one record into the variable lv string and it will be put into this one okay now suppose this is done now we'll write the raised notice so that we can see here it is notice if you don't know about the raised notice i have already made one video regarding the days notice their parameters and all you can check that one as also I have given the link in the video description and string so it will give me the outputs okay in a loop so this is done now let's write the end loop it's better to have the close also close cursor one so I have closed the cursor here and also since I am writing this code within the begin and end block I should have one exception handling as well so I'm writing exception suppose I'm writing when others then this is the common exception type to handle all the exceptions and here also i'm giving raise notice and a suitable message you can print like some something went wrong fine so our procedure is ready here and this procedure does work when the executions start it will fetch the record from the actor table in in a cursor okay so I am opening the cursor and in the loop I'm fetching records in record one by one and storing in REC one variable which is of a record type as I have defined here so now next next thing what I'm doing I have taken one character varying variable here and I'm storing data over here okay and then I'm showing that like displaying data using the raise notice so now let's create this procedure okay the procedure is created now we need to call the procedure to call the procedure i have already made many videos how to call the procedure call simple underscore cursor so there there is no input parameters so we can call like this only okay so i have selected this much only because i don't want to execute all those things so i'm highlighting these things and executing it okay so uh here are all the data that uh that i wanted to show you from actor table for actor id one first name is this and last name is this so let's verify these things from table okay so here is the output this is the actor id one name first name and last name actor id two first name last name so let's verify if the same is here using our loop that we had created so yeah it it's same both side you can also verify okay so if you if you don't have this dvd rental database in your system so you can set up this one also i'm giving the link in the video description you can use that so uh this much for the video in the next video we'll see other type of cursor and we'll cover multiple cursors that are available in postgres sql so don't forget to subscribe my channel to get the notification and also check out other videos from the playlist and the channel regarding the postgres sql and if you have any doubts you you are open to ask me in the comment box i'll try my best to answer them okay so let's meet in the next video tell them take care bye