📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Cursors

2022-06-02 •Youtube

Detailed Notes

1. Cursors

DELIMITER // CREATE PROCEDURE cursorExample ( ) BEGIN DECLARE done INTEGER DEFAULT 0; DECLARE theName varchar(100) DEFAULT "";

-- declare cursor DEClARE curName CURSOR FOR SELECT username FROM app_user;

-- declare NOT FOUND handler DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN curName; myLoop: LOOP FETCH curName INTO theName; IF finished = 1 THEN LEAVE myLoop; END IF;

-- provide output select theName; END LOOP myLoop;

-- Clean up when we are done CLOSE curName;

END// DELIMITER ;

DELIMITER // CREATE OR REPLACE PROCEDURE cursorExample ( ) BEGIN DECLARE done INTEGER DEFAULT 0; DECLARE theName varchar(100) DEFAULT ""; DECLARE output varchar(1000) DEFAULT "";

-- declare cursor DECLARE myCursor CURSOR FOR SELECT username FROM app_user;

-- declare NOT FOUND handler DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN myCursor; myLoop: LOOP FETCH myCursor INTO theName; IF done = 1 THEN LEAVE myLoop; END IF;

-- provide output set theName = concat(theName,", "); set output = concat(output,theName); END LOOP myLoop;

-- Clean up when we are done CLOSE myCursor; select output;

END// DELIMITER ;

DELIMITER // CREATE OR REPLACE PROCEDURE cursorExample ( ) BEGIN DECLARE done INTEGER DEFAULT 0; DECLARE theName varchar(100) DEFAULT ""; DECLARE theLogin varchar(100) DEFAULT ""; DECLARE output varchar(1000) DEFAULT "";

-- declare cursor DECLARE myCursor CURSOR FOR SELECT userName,userLogin FROM app_user;

-- declare NOT FOUND handler DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN myCursor; myLoop: LOOP FETCH myCursor INTO theName,theLogin; IF done = 1 THEN LEAVE myLoop; END IF;

-- provide output set theLogin = concat(" [",theName,"]"); set theName = concat(theName,theLogin," | "); set output = concat(output,theName); END LOOP myLoop;

-- Clean up when we are done CLOSE myCursor; select output;

END// DELIMITER ;

Transcript Text
[Music]
one well hello and welcome back we are
continuing our sql tutorials and most of
the focus on mysql and mariadb is our
test environment
and today we're continuing looking at
storm procedures and some of the things
we can do there
and we're going to get into cursors
now for this one
if i can find my document here
um i'll blow this up a little bigger
we're going to and what a cursor is
is a
a pointer for a set if you think about a
uh like a number of items and it you
know we've seen cursors in many ways but
so let's say you've got like item one
two three four five these are our
records
cursor is basically like the current row
selector for that so when the curs if
there's a cursor for these five rows
if the cursor's pointing to this row
then you're going to that's sort of your
current context and then you can move
the cursor to the next row and the next
row on the next or the next row
which is really the point and the the
use with
our context with what we're talking
about what we're going to want to do is
we're going to get a selection we're
going to get a record set and then we're
going to use a cursor to walk through
that
and do some sort of processing
in our case
we're going to keep it sourced simple
so we've got this little stored
procedure that we're just going to call
cursor example we're not going to send
any parameters or anything
and we're going to start out with a
couple variables we declare one is done
and it's going to start at you know zero
it's gonna be basically our boolean so
if it's a one
then it's done one and done zero it's
not done
and then we're gonna have a storage
variable called the name that we're
gonna keep up with now to get it for uh
declaring cursor is pretty
straightforward as you just declare
um
i'm sure i did
miss my typing on it and you give it a
name so i'm just going to call it so
this is just cursor name
or actually let me call it
my cursor
just to make it
and it's and you declare it as a cursor
for
which means what is it cursoring what's
the record set in this case i'm just
going to select username from app user
i could do
something more complicated but for now
we're going to keep it really simple
we're going to have a not found
handler which is basically says hey if i
don't find something so when i try to do
something with the cursor
as in if i go to next record
and it's not found then i'm going to set
done equal to 1 here so that just means
that hey if i get to the end of the the
record set i'm done
and then in order to
execute it basically we call an open
so i'm going to do an open on my cursor
i just want to make sure i cut up my
other thing and i'm going to kind of i'm
going to have a thing i'm just going to
call it my loop
in this loop
i am going to
fetch
uh
this is
my cursor
into the name which is my little
temporary variable
and uh if i'm finished
i'm sorry if done
so if i'm done which means if i get a
you know right here so to continue it
doesn't exit i could just blast out of
here if i don't find it what i'm going
to be you know do a nice little
clean
uh leave of this store procedure
so if i'm done if i've run off the end
of my list
my record set then i'm just going to
leave my loop
otherwise i'm going to select the name
and uh here's the end of my loop and
then i'm just go i've got a
we did an open up here for the cursor
and then when we're done we want to
close it
so we start open our connection and then
we close the connection
so if i take that
and let's go here
let's make sure hopefully i got
everything right
and i can do source
i got sequel let's see what it does up
uh undefined cursor her name
uh
where did i do oh
i did not change it here
all right cursor
let's go over here
oh i didn't get that
let's do it this way my cursor
i accidentally told it to save which i
didn't want to do
and let's try that again okay so now
if i call
cursor example
then what i'm doing here is i'm going to
see user1234 and let's let me
do that select for you
so i have
user1234 my users and it's going to come
through each time i'm just doing a
select here
now i can do something more
interesting
so i could do um
let's move him
outside of
the cursor
uh
let's do that
oh
except for i don't think it's gonna like
that concatenation but we're gonna find
out in a second i always forget that
piece
so now oh do i create a replace
uh nope
so i need to do or replace
and now if i come back here and source
it
okay now it's going to complain because
that doesn't
uh that's not how we concatenate i do
want to do
um
i believe it's this
oh i guess i'm doing it into the name
so let's call this
i'm gonna change this a little bit
because now we're gonna have to call
this output
and we'll make it a
larger like i don't know 1000 we'll make
a nice long line
and then output
equals concatenate
output comma the name kind of that
i think i can do it that way and if i do
select output
see how that looks
still didn't like it because it's not
that concatenate
oh sorry it's not concatenate i always
forget it is concat
and let's take that
and flip him over to
oh wait let's go here
put that in here just so we've got it in
our notes
uh let's see
oh and we're probably i don't think he
likes it doing it with three so first
we're gonna do that um
let's do this
equals
get that we're gonna concatenate the
name
there
and then we're gonna do that
and let's see if that is a little more
friendly nope still doesn't like it
because let's see what did we miss here
oh i forgot to do our sets
so we do that
and now we run it then we're good if we
try to do that call somewhere back here
now
what we're doing uh let me
blow this uh let me do two things okay
take this
and
replace it
and let me put it over here
so we can see it a little better
so now what i did is i declared this
output and i should probably call it
something else but we'll do that for now
um
and then instead of doing the select
each time like i did this time what i do
is each time i come through
i concatenate a comma to the name
and then concatenate that to the output
and then i get done i do one
select and i have my user list here
so i can do something more complicated
so i can do
let's do this
and let's do
so what i want to do here let's play
around with this a little bit
let's do that guys let's do
oh let's do uh username and user login
i'm gonna use your login
well this should be a comma
name
the login
and then
uh
that's all good i'm going to search that
into name comma
log in
and then let's do this
let's get really fancy
so the name equal
let's see
let's see if i can do this i think i can
do it this way
oh
and then i want that to be
in
let's just do a pipe just because
oh
so let's play with that let's see how
that does for us
whoops that's not what i want this is
what i want
so now when we call it
we've so
i'm sorry i flew right through that so
what i'm doing now so now what i'm going
to see is for each row i'm seeing the
username
and the uh the user log in in brackets
there beside it so what i did and i
could
probably break these out make them even
you know do a one-liner here but now um
actually let's flip it over here
oh and let's go ahead and tack it to the
end here so it's in our notes
so now what we're doing
and we can do this with any number of
fields
but what we're gonna do is now say okay
i've got two different values so i've
got two columns i'm selecting for my app
user
and then all i'm going to do is i just
fetch
from my cursor into
whatever fit you know as many variables
as i need to match those
fields that i'm pulling in the select
and so in this case all i'm doing is
coming in is saying all right
i'm going to come in and set up my um
instead of my little string here of
wrapping the log in with a couple
brackets and then
appending that to the name so that we
get username
and then log in
and then just put some brackets to do
that
now i could do that i could do selects i
could do all kinds of different things
that i wanted to as far as wheeling and
dealing with that
i could also take that and turn around
and use that as something you know grab
one of those values and then try to grab
a value from a different table or
or
could do that
for example and
for each um each record like in the app
user i could run one you know i could go
through and with each one
call
a
stored procedure
on that
particularly like if we do an update or
something like that
there's some things that we can do that
is your normal
uh working with loops and collections or
you know result sets
so
cursors become pretty powerful if you
want to
start really wheeling and dealing with
your data within your stored procedure
and
there's a lot of things that we can do
with them that help us
as far as like performance is concerned
and some other things
because we can we could do
and we don't this can have a where
clause this can be any our cursor can be
any select so we can get very
complicated
in our cursor and how we
pick our
data that we're going to be working with
so that gives us so an introduction to
cursors and working with them having a
couple fields how do we make that work
and how do we
integrate that into stored procedures
and that covers what i wanted to cover
for today for this uh session so
we will wrap this one up
and uh we're just gonna keep continuing
working our way through the various
things that are out there playing around
with some of these
um i don't know how far we are into this
as i said we've still got i know a few
more than a few more topics so
just keep coming back we're gonna keep
throwing these things out there and uh i
have changed it up
uh initially it was day one day two day
three and that's still sort of in my
notes but
i'm trying to put the now i'm switching
it over so you should be able to see
sort of the topic or a primary topic as
part of the
uh the name of the session if you're out
on youtube and hopefully that'll help
when you're doing it using this as like
a reference or something like that
that being said we'll wrap this one up
so go out there and have yourself a
great day a great week and we will talk
to you
next time
you
Transcript Segments
0.43

[Music]

27.039

one well hello and welcome back we are

29.599

continuing our sql tutorials and most of

32.8

the focus on mysql and mariadb is our

36

test environment

37.44

and today we're continuing looking at

40.399

storm procedures and some of the things

41.92

we can do there

43.28

and we're going to get into cursors

46.48

now for this one

49.28

if i can find my document here

51.76

um i'll blow this up a little bigger

55.92

we're going to and what a cursor is

59.52

is a

61.6

a pointer for a set if you think about a

65.28

uh like a number of items and it you

67.84

know we've seen cursors in many ways but

69.68

so let's say you've got like item one

71.119

two three four five these are our

72.72

records

74.24

cursor is basically like the current row

77.28

selector for that so when the curs if

80.56

there's a cursor for these five rows

83.52

if the cursor's pointing to this row

85.68

then you're going to that's sort of your

87.2

current context and then you can move

89.28

the cursor to the next row and the next

90.96

row on the next or the next row

93.04

which is really the point and the the

95.2

use with

96.88

our context with what we're talking

98.84

about what we're going to want to do is

101.36

we're going to get a selection we're

102.88

going to get a record set and then we're

104.799

going to use a cursor to walk through

106.64

that

107.439

and do some sort of processing

111.52

in our case

112.799

we're going to keep it sourced simple

115.36

so we've got this little stored

116.32

procedure that we're just going to call

117.439

cursor example we're not going to send

119.119

any parameters or anything

121.04

and we're going to start out with a

122.88

couple variables we declare one is done

125.92

and it's going to start at you know zero

128.319

it's gonna be basically our boolean so

130

if it's a one

132.48

then it's done one and done zero it's

135.28

not done

137.04

and then we're gonna have a storage

138.08

variable called the name that we're

139.76

gonna keep up with now to get it for uh

143.76

declaring cursor is pretty

144.879

straightforward as you just declare

148.08

um

149.12

i'm sure i did

150.56

miss my typing on it and you give it a

152.64

name so i'm just going to call it so

154.48

this is just cursor name

156.64

or actually let me call it

158.319

my cursor

160.239

just to make it

162.08

and it's and you declare it as a cursor

164.8

for

165.68

which means what is it cursoring what's

167.519

the record set in this case i'm just

169.28

going to select username from app user

172.959

i could do

175.28

something more complicated but for now

177.04

we're going to keep it really simple

180

we're going to have a not found

182.64

handler which is basically says hey if i

184.8

don't find something so when i try to do

187.44

something with the cursor

190.239

as in if i go to next record

193.2

and it's not found then i'm going to set

195.28

done equal to 1 here so that just means

197.84

that hey if i get to the end of the the

199.68

record set i'm done

202.879

and then in order to

205.12

execute it basically we call an open

208.4

so i'm going to do an open on my cursor

216.799

i just want to make sure i cut up my

217.92

other thing and i'm going to kind of i'm

218.959

going to have a thing i'm just going to

219.68

call it my loop

221.04

in this loop

222.56

i am going to

225.519

fetch

227.04

uh

228

this is

232.959

my cursor

238.56

into the name which is my little

240.08

temporary variable

243.2

and uh if i'm finished

245.519

i'm sorry if done

250

so if i'm done which means if i get a

252.64

you know right here so to continue it

254.56

doesn't exit i could just blast out of

256.4

here if i don't find it what i'm going

257.759

to be you know do a nice little

259.84

clean

260.799

uh leave of this store procedure

264.16

so if i'm done if i've run off the end

265.68

of my list

267.28

my record set then i'm just going to

269.28

leave my loop

271.52

otherwise i'm going to select the name

274.8

and uh here's the end of my loop and

276.96

then i'm just go i've got a

279.04

we did an open up here for the cursor

282.4

and then when we're done we want to

283.6

close it

285.199

so we start open our connection and then

286.56

we close the connection

289.36

so if i take that

291.68

and let's go here

293.44

let's make sure hopefully i got

295.36

everything right

298.639

and i can do source

302.88

i got sequel let's see what it does up

305.199

uh undefined cursor her name

311.12

uh

312.24

where did i do oh

314.56

i did not change it here

317.84

all right cursor

321.12

let's go over here

329.52

oh i didn't get that

331.12

let's do it this way my cursor

335.28

i accidentally told it to save which i

336.96

didn't want to do

338.88

and let's try that again okay so now

343.199

if i call

344.56

cursor example

353.36

then what i'm doing here is i'm going to

355.039

see user1234 and let's let me

358.4

do that select for you

365.44

so i have

367.72

user1234 my users and it's going to come

370

through each time i'm just doing a

371.44

select here

373.6

now i can do something more

376.16

interesting

377.36

so i could do um

380.479

let's move him

382.479

outside of

385.36

the cursor

396.4

uh

397.36

let's do that

401.68

oh

402.88

except for i don't think it's gonna like

404.16

that concatenation but we're gonna find

405.52

out in a second i always forget that

407.6

piece

411.44

so now oh do i create a replace

413.919

uh nope

415.12

so i need to do or replace

421.36

and now if i come back here and source

423.919

it

424.72

okay now it's going to complain because

426.08

that doesn't

427.199

uh that's not how we concatenate i do

429.68

want to do

433.599

um

437.44

i believe it's this

448.24

oh i guess i'm doing it into the name

452.639

so let's call this

455.28

i'm gonna change this a little bit

456.479

because now we're gonna have to call

457.44

this output

461.199

and we'll make it a

462.84

larger like i don't know 1000 we'll make

465.599

a nice long line

469.44

and then output

472.8

equals concatenate

476.84

output comma the name kind of that

482.879

i think i can do it that way and if i do

484.479

select output

488

see how that looks

492

still didn't like it because it's not

494

that concatenate

496.639

oh sorry it's not concatenate i always

498.96

forget it is concat

503.039

and let's take that

504.96

and flip him over to

509.44

oh wait let's go here

514.56

put that in here just so we've got it in

516.399

our notes

521.519

uh let's see

525.04

oh and we're probably i don't think he

526.8

likes it doing it with three so first

529.2

we're gonna do that um

533.2

let's do this

537.279

equals

541.04

get that we're gonna concatenate the

542.88

name

544.16

there

545.6

and then we're gonna do that

547.519

and let's see if that is a little more

550

friendly nope still doesn't like it

552

because let's see what did we miss here

554.48

oh i forgot to do our sets

558.16

so we do that

560

and now we run it then we're good if we

561.92

try to do that call somewhere back here

565.839

now

567.2

what we're doing uh let me

569.36

blow this uh let me do two things okay

572.16

take this

573.279

and

574.24

replace it

576.399

and let me put it over here

578.959

so we can see it a little better

581.12

so now what i did is i declared this

582.72

output and i should probably call it

584.56

something else but we'll do that for now

587.279

um

588.48

and then instead of doing the select

590.24

each time like i did this time what i do

591.92

is each time i come through

593.839

i concatenate a comma to the name

598.24

and then concatenate that to the output

600.32

and then i get done i do one

602.839

select and i have my user list here

607.04

so i can do something more complicated

609.36

so i can do

613.04

let's do this

620.88

and let's do

624.56

so what i want to do here let's play

626.48

around with this a little bit

627.839

let's do that guys let's do

632.48

oh let's do uh username and user login

639.2

i'm gonna use your login

641.279

well this should be a comma

646.24

name

647.68

the login

652.64

and then

653.839

uh

655.76

that's all good i'm going to search that

657.519

into name comma

660.24

log in

664.64

and then let's do this

671.92

let's get really fancy

677.2

so the name equal

679.36

let's see

687.44

let's see if i can do this i think i can

689.279

do it this way

691.44

oh

695.04

and then i want that to be

702.16

in

707.92

let's just do a pipe just because

715.36

oh

716.639

so let's play with that let's see how

718.48

that does for us

722.32

whoops that's not what i want this is

723.76

what i want

730.639

so now when we call it

734.399

we've so

735.92

i'm sorry i flew right through that so

737.279

what i'm doing now so now what i'm going

738.48

to see is for each row i'm seeing the

739.92

username

741.2

and the uh the user log in in brackets

744.16

there beside it so what i did and i

746.48

could

747.44

probably break these out make them even

749.2

you know do a one-liner here but now um

751.92

actually let's flip it over here

756

oh and let's go ahead and tack it to the

758

end here so it's in our notes

761.44

so now what we're doing

763.6

and we can do this with any number of

765.68

fields

766.72

but what we're gonna do is now say okay

768.32

i've got two different values so i've

769.92

got two columns i'm selecting for my app

772.88

user

774

and then all i'm going to do is i just

775.279

fetch

776.32

from my cursor into

778.8

whatever fit you know as many variables

780.56

as i need to match those

782.72

fields that i'm pulling in the select

786

and so in this case all i'm doing is

787.36

coming in is saying all right

788.959

i'm going to come in and set up my um

792.88

instead of my little string here of

795.36

wrapping the log in with a couple

797.2

brackets and then

799.12

appending that to the name so that we

801.6

get username

803.04

and then log in

804.639

and then just put some brackets to do

806

that

807.519

now i could do that i could do selects i

809.04

could do all kinds of different things

810.24

that i wanted to as far as wheeling and

812.16

dealing with that

813.519

i could also take that and turn around

815.76

and use that as something you know grab

817.839

one of those values and then try to grab

821.199

a value from a different table or

823.76

or

824.639

could do that

826

for example and

828.399

for each um each record like in the app

831.76

user i could run one you know i could go

834.079

through and with each one

836.16

call

837.199

a

838.16

stored procedure

839.6

on that

840.8

particularly like if we do an update or

842.56

something like that

844.399

there's some things that we can do that

846.24

is your normal

847.68

uh working with loops and collections or

850.639

you know result sets

852.639

so

853.519

cursors become pretty powerful if you

855.519

want to

857.519

start really wheeling and dealing with

858.959

your data within your stored procedure

861.6

and

862.56

there's a lot of things that we can do

864.079

with them that help us

865.839

as far as like performance is concerned

867.44

and some other things

869.6

because we can we could do

872

and we don't this can have a where

873.44

clause this can be any our cursor can be

875.44

any select so we can get very

878

complicated

879.519

in our cursor and how we

882.16

pick our

883.279

data that we're going to be working with

885.839

so that gives us so an introduction to

888.399

cursors and working with them having a

890.56

couple fields how do we make that work

894.16

and how do we

895.519

integrate that into stored procedures

898.079

and that covers what i wanted to cover

899.92

for today for this uh session so

903.76

we will wrap this one up

905.92

and uh we're just gonna keep continuing

907.6

working our way through the various

908.72

things that are out there playing around

910.079

with some of these

911.44

um i don't know how far we are into this

913.68

as i said we've still got i know a few

915.6

more than a few more topics so

917.68

just keep coming back we're gonna keep

918.8

throwing these things out there and uh i

920.88

have changed it up

922.72

uh initially it was day one day two day

924.72

three and that's still sort of in my

926.16

notes but

928.32

i'm trying to put the now i'm switching

930.56

it over so you should be able to see

932.399

sort of the topic or a primary topic as

935.279

part of the

936.639

uh the name of the session if you're out

938.8

on youtube and hopefully that'll help

940.8

when you're doing it using this as like

942.32

a reference or something like that

944.56

that being said we'll wrap this one up

946.16

so go out there and have yourself a

947.44

great day a great week and we will talk

950.24

to you

951.44

next time

968.72

you