📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Day 14

2022-05-19 •Youtube

Detailed Notes

1. In, Out, InOut 2. if then else end if 3. case when then else

delimiter // create or replace procedure tutorial.multiplier(in x int, out y int, inout z int) BEGIN select x * z into y; select x * x into z; END; // delimiter ;

set @second = 5; call multiplier(2,@result,@second);

select @result;

create or replace procedure tutorial.multiplier(in x int, out y int, inout z int) BEGIN select x * z into y; select x * x into z; END; // delimiter ; call multiplier(2,4,6);

set @first = 4; call multiplier(2,@first,6); set @second = 6;

call multiplier(2,@first,@second);

delimiter // create or replace procedure tutorial.greeting(in name varchar(20), in flag varchar(1)) BEGIN if flag = "M" then select concat("Hello Mr. ",name); ELSE select concat("Hello Ms. ",name); end if; END; // delimiter ;

delimiter // create or replace procedure tutorial.greeting(in name varchar(20), in flag varchar(1)) BEGIN CASE WHEN flag = "M" then select concat("Hello Mr. ",name); WHEN flag = "F" then select concat("Hello Ms. ",name); ELSE select concat("Hello ",name); end case; END; // delimiter ;

Transcript Text
[Music]
well hello and welcome back we are
continuing our season where we're
looking at sql stuff uh working our way
through all kinds of cool little
stateful things
this episode we're continuing uh focused
on mysql and mariadb mostly
and we're getting looking at stored
procedures sort of the month of stored
procedures
and this episode we're going to look at
a couple different things
we're going to review
basically go back to parameters
before we had end parameters we're going
to look at in out and in out parameters
and then we're going to take a look at
some logical stuff such as if statements
and things of that nature
so that being said
i want to start with a little stored
procedure here
and we're gonna do this thing called
multiplier let me see if i can blow this
guy up can i do that
okay so we'll go back to here so what
we're doing in this one
is it's just like we've done before so
let's create a replace procedure
and it's going to be called multipliers
this is a new one
we've got three variables we've got n
well i'm sorry we've got x which is an n
it's coming in and it's an integer
we've got y
which is an out
and it's an integer
and we have z which is an in out which
is an integer so we've got three
integers coming in
and what we're going to do with them is
we're going to select
x times y
i'm sorry x times z into the y variable
and x times x into the z variable
now first let me talk about in out and
in out so n variables
are values that are going to come in we
will not pass them out so you could do
like a call
my proc
and you could give it a number which
we've done before like one if it's an n
variable
if it's an out
and i do that it's going to give me an
error because it needs to be able to
send it back out into something
and it's
i'm sorry if it's out
it's a variable it's basically an empty
variable it ignores what's sent in
and then if it's in out it can utilize
what comes in
but it also will rewrite it so it has to
be a variable
i'll show you what that looks like as we
go through these a little bit
so so right now we're just doing a call
multiplier
and
we've got three values so let's just do
2 times 4 times 6 and let's see what it
tells me
now in this case
here we go out or in out argument 2
is not a variable
so argument 2 has to be a variable
so we're going to do
set
at
first
equals four
and then
we're going to do at first
let's see what it does
and so now we sent a variable in but now
the second one is complaining about that
because out and in out both have to be
a variable so the second is going to be
we're going to call it second well
we're going to set it to 6
and now when we call it
at first a second boom it works
so since we didn't display anything we
need to take a look at these so now what
we can do is we can look at first which
is the y parameter
and we can see what is its value now
oops i have to put semicolon it's 12.
which is
2 times 6 is 12. and then if we do
second
it should be x squared so 4 right yep
and it's 4.
so it's pretty easy to
set up our variables that we just have
to remember if they are and if we're
trying to keep it simple then you want
them all ends and then you're just going
to
have one value maybe that comes out but
you hate may have a bunch of things
along the way that you're having to
work with
maybe row counts and and stuff like that
and
particularly you may want to overwrite
some of that stuff and pass it back
through those
typically in outs are very
confusing for people to work with
particularly as a you know the people
that are calling those procedures so
it's better to do one or the other
but that can sometimes be
very i can get you some very long
signatures
that you may not want to do but
there are definitely common uses for
such things for example if we want to
insert a record
then maybe we want to insert we give it
the values that we want to insert but we
have an out variable that is the new
record id there's something along those
lines
or it could be a you know a pass fail or
a confirmation value or something like
that there's a lot of different things
we can do
so
those are in and out variables next i
want to look at is uh
let me just take this whole
that well let me delete this
i'm going to take that whole thing
because i'm gonna throw it into our
notes and then i'll clean it up later
one second while i do this
okay
so i've got that for now
another thing i want to do i want to
look at an if then else so let's go
build and if then else real quick
so let's do and let's keep this simple
so i'm going to say
i'm going to keep this really simple so
i'm going to do
in
name
is a varchar
20.
oh let's do this
and
in
gender
is
varchar is a char one it's gonna just be
an m or f
if you want something else then that's
okay
well let's do that
we'll take care of that a minute
uh so in gender and in name so now we're
gonna do an f so we're gonna say if
gender
gender
equals
m
then
select
hello
mr
plus
name
and let's see what that does oh and then
we're gonna do an end if let's just do
that right now
so this is gonna be greeting
so we're gonna do greeting
and we've got a name and a gender okay
so let's go back over here
and whoa
okay so
it took a little bit uh just basically
had some sort of neat little
uh surprise character or something like
that so let's get back to what we were
working on here
took me a minute to fix that so i'm
gonna do select concat
uh hello mister
and then i'm gonna get it
sorry uh
name
so
let's see if this works now that should
work
okay so it does
so now if i call
greeting
and they're both ends so the name is
going to be
uh mr bob uh one's mickey jones
and we're gonna make it an m
oops
and so it's going to say hello mr jones
if we give it something else
then it gets nothing because
there is no else there so let's take
care of that so let's say
and that would also been
if i'd said it was female nope nothing
so let's take care of the female case
so for that one
it's gonna be hello miss
and so now
oh i need to replace it
ah dang it
i did that again ah
okay
oh why do you keep doing this to me okay
all right
okay let's do this
and let's see if this will run a little
better
all right we're going to change it we're
going to do avi
current dot sql
and
so that could be pretty
but it's going to work
whoops
let's get into here
okay
so that's current now let's see if we do
this
okay so now let's call it
uh let's go back to our call where was
that call call call call
what a mess
so if we call it with
an f we get hello mrs jones
if we call it with an m
again hello mr jones if we call it with
something else say other
then we get
mrs jones still it's not taking that
into account so what we can do instead
let me see if i can fix it this way
instead of this now what we're going to
do is we're going to do a case
and a case statement looks like
basically when
then and there's an else at the end so
we can sort of make it simple so we're
going to say when flag
equals that
when
flag equals
f
then
that
and then we're going to do an else
and it's just going to say hello
jesus to make it simple
now if we take that and source it
and now we call it if we do an o it's
just going to say hello jones if we do a
female
it's gonna say hello misses if we do a
male it'll be hello mister if we do
lowercase because it's gonna be case
sensitive
no it it's not good
if i didn't x that's just going to go
hello jones if i do empty it's going to
be hello jones if i do a null it's going
to tell me i need a perimeter so
it's not
too much different to do a so we can do
an if and when to do a case
it's just going to be a series of wins
sort of like an if-then-else
but it's just going to be you know if
than that or if then else if that you
would normally see and no with your case
let's go to something
you can't see the speed but note that
with a case
there's going to be an n case
and when you do an if
there is going to be
an end if
so let me take that and copy that over
here to make sure we've got it in our
little list o things
and so there's a couple of ways for us
to now get a little more
fancy basically with our procedures
through store procedures we can use n
variables out variables ins and outs
and now we've got some logic we can do
between ifs and case statements
so we are slowly building on this thing
and we'll continue to do so
in the next in future episodes that
being said i think it's time to wrap it
up apologies for the confusion today
some sort of cut and paste kicked my
butt but
that happens sometimes we all get bit
sometimes by very odd little things
and
that just leaves us to wrap it 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.199

well hello and welcome back we are

28.96

continuing our season where we're

30.4

looking at sql stuff uh working our way

32.96

through all kinds of cool little

33.92

stateful things

35.2

this episode we're continuing uh focused

37.2

on mysql and mariadb mostly

40.079

and we're getting looking at stored

41.84

procedures sort of the month of stored

44

procedures

45.2

and this episode we're going to look at

47.039

a couple different things

48.96

we're going to review

50.239

basically go back to parameters

52.64

before we had end parameters we're going

54.16

to look at in out and in out parameters

57.84

and then we're going to take a look at

58.8

some logical stuff such as if statements

61.28

and things of that nature

63.52

so that being said

66.479

i want to start with a little stored

68.56

procedure here

71.04

and we're gonna do this thing called

72.88

multiplier let me see if i can blow this

75.36

guy up can i do that

77.759

okay so we'll go back to here so what

79.68

we're doing in this one

82.24

is it's just like we've done before so

83.6

let's create a replace procedure

85.6

and it's going to be called multipliers

87.2

this is a new one

88.32

we've got three variables we've got n

90.799

well i'm sorry we've got x which is an n

93.28

it's coming in and it's an integer

95.84

we've got y

97.439

which is an out

98.96

and it's an integer

101.36

and we have z which is an in out which

103.84

is an integer so we've got three

104.96

integers coming in

107.04

and what we're going to do with them is

108.64

we're going to select

110.399

x times y

112.32

i'm sorry x times z into the y variable

115.68

and x times x into the z variable

119.36

now first let me talk about in out and

121.759

in out so n variables

124.32

are values that are going to come in we

125.759

will not pass them out so you could do

128

like a call

129.84

my proc

131.52

and you could give it a number which

132.879

we've done before like one if it's an n

134.959

variable

136.319

if it's an out

137.92

and i do that it's going to give me an

139.76

error because it needs to be able to

141.92

send it back out into something

144.319

and it's

145.28

i'm sorry if it's out

147.92

it's a variable it's basically an empty

149.28

variable it ignores what's sent in

151.92

and then if it's in out it can utilize

154.239

what comes in

155.519

but it also will rewrite it so it has to

158.08

be a variable

159.84

i'll show you what that looks like as we

161.12

go through these a little bit

162.48

so so right now we're just doing a call

165.44

multiplier

167.599

and

169.12

we've got three values so let's just do

170.56

2 times 4 times 6 and let's see what it

173.04

tells me

174.319

now in this case

176.56

here we go out or in out argument 2

179.599

is not a variable

181.12

so argument 2 has to be a variable

183.68

so we're going to do

185.76

set

187.599

at

188.56

first

190.48

equals four

193.68

and then

195.36

we're going to do at first

197.36

let's see what it does

199.28

and so now we sent a variable in but now

201.44

the second one is complaining about that

203.12

because out and in out both have to be

208.239

a variable so the second is going to be

210.4

we're going to call it second well

213.12

we're going to set it to 6

215.44

and now when we call it

220.799

at first a second boom it works

223.599

so since we didn't display anything we

226.48

need to take a look at these so now what

228.08

we can do is we can look at first which

230.799

is the y parameter

232.959

and we can see what is its value now

236.879

oops i have to put semicolon it's 12.

240.159

which is

242.56

2 times 6 is 12. and then if we do

245.76

second

250.959

it should be x squared so 4 right yep

254.48

and it's 4.

256.799

so it's pretty easy to

258.72

set up our variables that we just have

260.16

to remember if they are and if we're

262.32

trying to keep it simple then you want

264.16

them all ends and then you're just going

265.68

to

266.4

have one value maybe that comes out but

268.639

you hate may have a bunch of things

270

along the way that you're having to

272.32

work with

273.759

maybe row counts and and stuff like that

276.56

and

278.72

particularly you may want to overwrite

280.16

some of that stuff and pass it back

281.52

through those

282.56

typically in outs are very

284.479

confusing for people to work with

287.199

particularly as a you know the people

289.04

that are calling those procedures so

290.479

it's better to do one or the other

293.36

but that can sometimes be

295.52

very i can get you some very long

297.84

signatures

298.96

that you may not want to do but

302.479

there are definitely common uses for

304

such things for example if we want to

305.759

insert a record

307.28

then maybe we want to insert we give it

309.12

the values that we want to insert but we

311.039

have an out variable that is the new

312.96

record id there's something along those

315.12

lines

316.32

or it could be a you know a pass fail or

318.24

a confirmation value or something like

319.6

that there's a lot of different things

320.56

we can do

322.08

so

323.52

those are in and out variables next i

326.479

want to look at is uh

330.8

let me just take this whole

333.039

that well let me delete this

336.4

i'm going to take that whole thing

337.52

because i'm gonna throw it into our

338.4

notes and then i'll clean it up later

341.759

one second while i do this

344.56

okay

345.84

so i've got that for now

348.56

another thing i want to do i want to

349.68

look at an if then else so let's go

351.759

build and if then else real quick

355.36

so let's do and let's keep this simple

357.919

so i'm going to say

359.52

i'm going to keep this really simple so

361.039

i'm going to do

362.319

in

363.919

name

365.36

is a varchar

367.919

20.

370.8

oh let's do this

372.72

and

373.759

in

375.919

gender

377.44

is

379.039

varchar is a char one it's gonna just be

381.199

an m or f

383.28

if you want something else then that's

384.639

okay

386

well let's do that

387.199

we'll take care of that a minute

388.8

uh so in gender and in name so now we're

392.4

gonna do an f so we're gonna say if

396

gender

398.8

gender

400.24

equals

401.84

m

403.759

then

406

select

409.44

hello

411.52

mr

415.68

plus

416.72

name

418.8

and let's see what that does oh and then

420.08

we're gonna do an end if let's just do

422.4

that right now

424.72

so this is gonna be greeting

430.319

so we're gonna do greeting

432.479

and we've got a name and a gender okay

434.88

so let's go back over here

437.919

and whoa

439.919

okay so

441.759

it took a little bit uh just basically

443.36

had some sort of neat little

445.44

uh surprise character or something like

448

that so let's get back to what we were

449.599

working on here

451.36

took me a minute to fix that so i'm

452.72

gonna do select concat

455.199

uh hello mister

458.4

and then i'm gonna get it

461.36

sorry uh

462.84

name

464.8

so

466.319

let's see if this works now that should

468.16

work

469.28

okay so it does

471.68

so now if i call

474.319

greeting

475.84

and they're both ends so the name is

478

going to be

478.879

uh mr bob uh one's mickey jones

483.36

and we're gonna make it an m

488.479

oops

492.96

and so it's going to say hello mr jones

495.52

if we give it something else

497.919

then it gets nothing because

501.36

there is no else there so let's take

502.879

care of that so let's say

504.96

and that would also been

507.52

if i'd said it was female nope nothing

509.199

so let's take care of the female case

512

so for that one

514.479

it's gonna be hello miss

519.519

and so now

521.599

oh i need to replace it

528.08

ah dang it

529.68

i did that again ah

532.16

okay

541.12

oh why do you keep doing this to me okay

550.959

all right

554.16

okay let's do this

559.68

and let's see if this will run a little

561.76

better

577.519

all right we're going to change it we're

579.44

going to do avi

582.88

current dot sql

586.48

and

603.2

so that could be pretty

606.32

but it's going to work

608.16

whoops

611.279

let's get into here

618

okay

620

so that's current now let's see if we do

622.64

this

631.6

okay so now let's call it

634.399

uh let's go back to our call where was

635.839

that call call call call

639.92

what a mess

644.32

so if we call it with

646.64

an f we get hello mrs jones

649.76

if we call it with an m

652.56

again hello mr jones if we call it with

654.48

something else say other

657.6

then we get

659.2

mrs jones still it's not taking that

661.12

into account so what we can do instead

665.519

let me see if i can fix it this way

670.079

instead of this now what we're going to

671.68

do is we're going to do a case

675.44

and a case statement looks like

677.68

basically when

679.12

then and there's an else at the end so

685.04

we can sort of make it simple so we're

686.32

going to say when flag

688.399

equals that

691.92

when

695.2

flag equals

698.64

f

701.36

then

702.64

that

706.959

and then we're going to do an else

710

and it's just going to say hello

713.839

jesus to make it simple

721.6

now if we take that and source it

725.839

and now we call it if we do an o it's

728.399

just going to say hello jones if we do a

730.56

female

731.68

it's gonna say hello misses if we do a

733.839

male it'll be hello mister if we do

736.16

lowercase because it's gonna be case

737.6

sensitive

739.2

no it it's not good

741.92

if i didn't x that's just going to go

743.839

hello jones if i do empty it's going to

745.6

be hello jones if i do a null it's going

747.76

to tell me i need a perimeter so

750.88

it's not

752.399

too much different to do a so we can do

755.04

an if and when to do a case

758.399

it's just going to be a series of wins

759.839

sort of like an if-then-else

761.68

but it's just going to be you know if

762.959

than that or if then else if that you

765.2

would normally see and no with your case

768.399

let's go to something

769.839

you can't see the speed but note that

771.839

with a case

773.76

there's going to be an n case

775.36

and when you do an if

777.36

there is going to be

779.279

an end if

781.839

so let me take that and copy that over

784.32

here to make sure we've got it in our

786.72

little list o things

789.12

and so there's a couple of ways for us

790.48

to now get a little more

792.399

fancy basically with our procedures

795.68

through store procedures we can use n

797.44

variables out variables ins and outs

800.399

and now we've got some logic we can do

802.639

between ifs and case statements

806

so we are slowly building on this thing

808.72

and we'll continue to do so

810.8

in the next in future episodes that

813.12

being said i think it's time to wrap it

814.8

up apologies for the confusion today

817.36

some sort of cut and paste kicked my

818.72

butt but

820.56

that happens sometimes we all get bit

822.399

sometimes by very odd little things

824.959

and

826

that just leaves us to wrap it up so go

827.68

out there and have yourself a great day

829.04

a great week and we will talk to you

831.76

next time

849.199

you