How to write a Cursor for PostgreSQL. Learn how to Open, Move, Fetch, Close., and Update/Delete DB.

  Рет қаралды 4,192

Software Nuggets

Software Nuggets

Күн бұрын

Пікірлер: 11
@SoftwareNuggets
@SoftwareNuggets 2 жыл бұрын
SOURCE CODE FOR VIDEO written by : Software Nuggets drop table public.department create table public.department ( deptId int not null, deptName varchar(50) not null, groupName varchar(50) not null, modDate timestamp not null, onSite bool not null default 'Y' ); insert into public.department(deptId,deptName,groupName,modDate) values (1,'Executive','Admin','2022-09-03 16:40'), (2,'Finance','Admin','2022-09-03 16:40'), (3,'Human Resources','Admin','2022-09-03 16:40'), (4,'Programming','MIS','2022-09-03 16:40'), (5,'Networking','MIS','2022-09-03 16:40'), (6,'Marketing','Admin','2022-09-03 16:40'), (7,'Quality','MIS','2022-09-03 16:40'), (8,'Documentation','MIS','2022-09-03 16:40'), (9,'Research','MIS','2022-09-03 16:40'), (10,'Help Desk','MIS','2022-09-03 16:40') create or replace function ShowReverse() returns void as $$ declare c1 refcursor; trow department%rowtype; begin raise notice 'start'; open c1 scroll for select * from department; fetch last from c1 into trow; loop raise notice 'found=%',found; exit when not found; raise notice 'prev=% % %',trow.deptId, trow.deptName, trow.groupName; fetch prior from c1 into trow; end loop; close c1; raise notice 'finished'; end; $$ language 'plpgsql'; select * from ShowReverse(); create or replace function ShowAllDepartments() returns void as $$ declare c1 refcursor; trow department%rowtype; work_onsite bool; begin raise notice 'start'; open c1 for select deptId, deptName,onsite from department order by random() fetch first 3 rows only; loop fetch next from c1 into trow; exit when not found; if trow.deptId % 2 = 1 then raise notice 'found=% %',trow.deptId,trow.deptName; update department set onsite = false, modDate = now() where deptId = trow.deptId; end if; end loop; close c1; raise notice 'finished'; end; $$ language 'plpgsql'; select * from ShowAllDepartments(); create or replace function DeleteWhenFalse() returns void as $$ declare c1 refcursor; trow department%rowtype; begin open c1 for select * from department; loop fetch next from c1 into trow; exit when not found; if trow.deptId % 2 = 1 then if trow.OnSite = 'f' then delete from department where deptId = trow.deptId; raise notice 'has been deleted: % %',trow.deptId, trow.onSite; end if; end if; end loop; end; $$ language 'plpgsql'; select * from DeleteWhenFalse();
@JorgeEscobarMX
@JorgeEscobarMX Жыл бұрын
Thank you. I love your playlist
@SoftwareNuggets
@SoftwareNuggets Жыл бұрын
Thanks Jorge. Appreciate the comment.
@MrGschecho
@MrGschecho 2 жыл бұрын
Thank you! this explanation really help me out!
@SoftwareNuggets
@SoftwareNuggets 2 жыл бұрын
Glad to hear this video was useful. Thanks for leaving a comment.
@hoperight5280
@hoperight5280 Жыл бұрын
Again, thank you for the effort; it's priceless. Is there any chance that you could make videos of store procedures and triggers as well?
@SoftwareNuggets
@SoftwareNuggets Жыл бұрын
I have a video already about triggers which is pretty good, please watch. Tell me what you want the stored procedure to perform. If you already have a database table, could make a video that is relevant. Thanks for the comment and trust for me to make a video for you.
@hoperight5280
@hoperight5280 Жыл бұрын
@@SoftwareNuggets I recall that I looked it up but I guess it fell off my radar. Thanks.
@SoftwareNuggets
@SoftwareNuggets Жыл бұрын
@@hoperight5280 have you viewed any of my Postgresql Shorts? many about Arrays and JSON. here is a link, if you are interested: kzbin.info/aero/PLRU_t-SgTrYgEyj0rCpDX30J0ombkf1kO
@escm-9db
@escm-9db Жыл бұрын
11:07 trivial but curious why we're printing 'prev=% % %' in the looping example. Is this supposed to stand for Previous row? Shouldn't it maybe be Current as it's going to print whichever row we're currently on?
@SoftwareNuggets
@SoftwareNuggets Жыл бұрын
Hey @j_esc9161, You are very correct. raise notice 'prev=% % %" should be "current row % % %". thanks for pointing this out. -- scott
How to write SQL Functions in PostgreSQL
21:01
Software Nuggets
Рет қаралды 32 М.
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49
Long Nails 💅🏻 #shorts
00:50
Mr DegrEE
Рет қаралды 20 МЛН
Правильный подход к детям
00:18
Beatrise
Рет қаралды 2,2 МЛН
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 105 МЛН
What is a Cursor? The Complete Python/PostgreSQL Course 2.0.
5:12
SQL Cursors - how and when to use them
7:45
James Oliver
Рет қаралды 64 М.
Postgres Triggers in a Nutshell
10:09
Troi
Рет қаралды 22 М.
#Triggers #Functions #PostgreSQL Tutorials #SQL Concepts
14:39
BestTechLearn
Рет қаралды 33 М.
Full Text Search PostgreSQL
18:13
Ben Awad
Рет қаралды 75 М.
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49