📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Signal and Resignal

2022-05-31 •Youtube

Detailed Notes

1. Signal 2. Resignal

DELIMITER //

CREATE OR REPLACE PROCEDURE RaiseSignal(in value int ) BEGIN IF(value != 1) THEN SIGNAL SQLSTATE '45123' SET MESSAGE_TEXT = 'Oops, that is not a 1'; END IF;

select 'End Of Program'; END//

DELIMITER ;

DELIMITER //

CREATE PROCEDURE resignalExample(IN numerator INT, IN denominator INT) BEGIN DECLARE division_by_zero CONDITION FOR SQLSTATE '22012';

DECLARE CONTINUE HANDLER FOR division_by_zero RESIGNAL SET MESSAGE_TEXT = 'Denominator cannot be zero'; IF denominator = 0 THEN SIGNAL division_by_zero; ELSE SET result := numerator / denominator; select result; END IF; END//

DELIMITER ;

Transcript Text
[Music]
well hello and welcome back we're
continuing our tutorial series with
mysql our
for focus really on sql although we're
you know using mysql and mariadb is our
examples
today
we are going to
look at signal and resignal we're sort
of continuing our
exceptions and error handling portion of
this
and we are
getting into a couple other ways that we
can we can work with them
the first thing we're going to work with
is we're going to walk through is
called signal
now the
syntax for signal
is actually pretty straightforward
and it is you're going to do signal sql
state
and you're going to give it a number
and then
most likely you're going to want to give
it message text as well otherwise it's
not very useful
so you're going to give some sort error
and this is where you want to raise
some error that you
you want to throw out to the user so
actually let me change this what i'm
going to do here
is in this procedure let me just clean
this up a little bit so it's a little
bit let's put that in the same row and
what we're going to do
is create this thing called raise signal
and we're going to give it a value and
right now we're just going to make it
really simple if the value is not equal
to 1 then it's going to say that does
not exist but instead of that we're
going to say
oops
that is not a one
and we're gonna go with that
oh
i hate when i do that
so i'm gonna take this
and throw it over my current because it
so i can do that
and then
if i jump into here and do source
current not sql
oh or exists because i need to do create
or replace
and let me fix that
here just so you can see it
because i was playing around with this
earlier so that's already there
so now we come here now if we source
okay he's good
so if i call raise signal
whoops if i spell it right
with the
one
then
nothing happens
if caught with a zero
oops that's not a one and it gives me
that error code
and so if we're looking here
and looking you know basically it's a if
so there's nothing there so if the value
is not equal to one
then it's going to give me this message
otherwise it does nothing
let's just do this let's do uh select
uh end of
program just to show you how this goes
oop then we'll do that but i do want to
do this
oh
let's go over here
just to
update my current
and now
if i source it
now if i call it with a zero
oops that is not one if i call it the
one
i get in a program so you can see here
that in this case
what happens is when i do that raise
i'm sorry that signal
when i signal
it does not give me
in the program it just says oops that's
not a one so signal bails out it
essentially raises an exception which is
this number and i can do let's call it
one two three
and let's go
where was my current let's change that
to like one two three
and let's uh source
signal with those zero one still works
fine if i sing with zero
then we can see that's
my return code
now the error
is it's a specific error because we've
raised this is something that sql is
going to give you but
because it gives you that id oops so
here it's a little bigger to see
because of the state it's going to
return that number so if you want to
have a custom error code
and
a message
then you can use signal
in order to do so
now
there are situations where
that could be a problem you don't want
to signal
and have something happen while you're
dealing with raising your exception
that gives you
that's where
resignal comes in
so in the resignal example
for this one we're going to have a
numerator and a denominator we're going
to do a division
and what we're going to do is we're
going to say if we see division by 0
then that's for if we hit this sql state
and we're going to declare our handler
which we've seen before so this is going
to be continue handler
and we're going to set
uh here
within this re-signal
we're going to set our
message
and the problem here is
is that when i come in here
if denominator equals zero and i do a
signal division by zero
then if i want to do anything within it
i have to essentially send it back up
otherwise
it's gonna it caused some
issues and that's why here i'm going to
get a signal but i also want to continue
it
so i'm
the flow is it's going to catch an error
and within that error
there is an exception but i'm basically
capturing that exception and so i need
to resign which means i need to pass
that back up the chain so rather than
just bailing out completely
because this is a continue
and because
i need to i want to
catch that exception if you think of
like a try catch i want to catch that
exception and then pass it up the chain
then i'm going to use a re-signal
instead
so in this case
let's see what oh i need to take this
let's make sure i get the whole thing
and throw that into my current
and then run that
uh results oops what did i do
oh i'm sorry in line
four
what did i do here uh
oh
declare results and
this is just going to be an ant
okay
i can change that my mistake missed one
in there
oh i need to group my creator replace
there we go so now i want to call it as
a resignal example with two numbers so
call resignal
example
and let's just do it uh two and one
how is that let's see numerator
denominator okay
so this should give us two over one
results two if we swap those two
should give us a half
uh oh it's giving us an int out my
mistake that should be a
float
now if we run current
there we go
and let's go back to the one we call it
two one and it's two so we're okay there
so we can
you know if we want to do i don't know
some number comma some smaller number
okay so we're gonna get our results so
that's working however
let's put let's start with a zero
on the numerator
and that's zero that's fine
but now if we do three comma zero
bam now we're getting the denominator
cannot be zero
so we have
uh in this oops so i need to look at it
so what we did is we came in here we
said hey i want to change the text
if otherwise let's see if i
get rid of that
i can't do it here
i get rid of that here
and get rid of my signal
when i run it uh set current
then
it's getting caught it's basically
getting just lost because i'm not
passing it back up
and so
uh this continue just says nope i'm not
going to do anything with it now if we
do uh let's do this
uh well
let's do
let's see what happens if we do select
result
regardless
in this case we do source
so we get denominator it cannot be zero
it bails out if we do it with a one
then it's going to come through and give
us our result
so we're still getting uh here because
it's a signal it was bailing out
before
um
so with this signal
we are rewriting the text if we don't
if we don't do it see remember if we
don't do it because this is a continue
handler
it just eats that example or that
there we go oh sorry re it eats that
exception
so it's going to come right through
because it's a continue handler now i
could say
if i just wanted to say
signal
then i'm going to get
here
i get that right
oh
then it's going to give me an issue
because
oh that's okay
because it's expecting that
signal that signal is always being
triggered and so it doesn't like that
so it's basically we're now just running
into a
syntax error
if we re-signal
then we're okay
and this is something we can play around
with a little bit as far as raising our
exceptions and dealing with them but
it's it's really and we'll see some more
of this as we get deeper into
uh some rather complex stored procedures
but this gives us a start so that we can
you know now between our prior
uh conversation when we talked about
exceptions in general and some of the
handlers whether it was a continue
handler or an exit
handler now we have the ability to also
signal specific errors which goes back
to here
[Music]
let's see
so we can do
exit handlers for specific
um or continue handlers for specific
errors
well
i can bring that up
and so actually i'll bring it over here
what's easier to read
where is that there you go
here then we can create our own
um
and we can set our signal state to a
specific spot we can start playing
around with that as well so we can
utilize
and
[Music]
basically
uh enhance or add to or even i mean just
i guess generally modify the
exception flow
we can use what's built in but we can
also change the messages we can
capture one do some work and then
continue on or capture it do some work
and then bail out
so it gives us
with these
this in the prior episode
we now have some tools
to do
very full-featured exception handling
which again
maybe it's not that big a deal
with the initial couple of store
procedures we've seen but fairly quickly
you'll see when you get into building
out stored procedures that there are
going to be situations where you're
going to want to bail out where you're
going to say hey i'm
i'm working on records that don't exist
or i'm spinning through a list of ids
and i'm looking things up based on that
and if one doesn't exist somewhere then
there's going to be an issue
it's actually could be for example a way
to
encode do
some sort of a foreign key relationship
as opposed to
doing it within
using the indexes and the keys
not sure why you would do that
but there are situations i think where
you would have something that is
depending on how tricky you want to get
it where you you're wanting to do some
sort of logic
based on
whether an id exists or not and it's not
really a foreign key because you're not
forcing it to exist but
you do want the ability to handle that
you want to have you know maybe the case
for example maybe you have a case where
if the foreign key doesn't exist
then you create that record uh you go
and create it link it up and then go
from there so there's
yeah i'm i'm sort of just throwing ideas
out at this point because once you get
into exceptions you're usually outside
of you're dealing with outliers anyways
even though they may be somewhat common
i guess outliers which
would make a little different but
you're basically saying hey here's a
couple of things that can happen i want
to be able to handle those conditions as
opposed to just
you know crashing
or
quietly doing nothing and then the user
has to figure out what's going on the
caller of that stored procedure has to
figure out what's going on this was a
little short but we've gone along the
last couple times so i figure we'll
we'll do one
and we will come back next time we're
just going to continue cranking our way
through
sql and learning how to use it better
but
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.279

well hello and welcome back we're

29.119

continuing our tutorial series with

31.679

mysql our

33.2

for focus really on sql although we're

35.6

you know using mysql and mariadb is our

39.28

examples

40.64

today

41.68

we are going to

43.2

look at signal and resignal we're sort

45.84

of continuing our

48.079

exceptions and error handling portion of

50.719

this

51.84

and we are

53.36

getting into a couple other ways that we

54.879

can we can work with them

57.12

the first thing we're going to work with

58.879

is we're going to walk through is

61.52

called signal

64.159

now the

65.199

syntax for signal

67.04

is actually pretty straightforward

69.2

and it is you're going to do signal sql

71.6

state

72.479

and you're going to give it a number

75.2

and then

76.159

most likely you're going to want to give

77.6

it message text as well otherwise it's

80.4

not very useful

81.84

so you're going to give some sort error

83.439

and this is where you want to raise

86.64

some error that you

88.479

you want to throw out to the user so

90

actually let me change this what i'm

91.759

going to do here

94

is in this procedure let me just clean

96.4

this up a little bit so it's a little

97.84

bit let's put that in the same row and

100.88

what we're going to do

102.399

is create this thing called raise signal

104.799

and we're going to give it a value and

106.88

right now we're just going to make it

107.84

really simple if the value is not equal

109.52

to 1 then it's going to say that does

111.84

not exist but instead of that we're

113.2

going to say

115.119

oops

116.56

that is not a one

120.56

and we're gonna go with that

123.119

oh

124

i hate when i do that

125.68

so i'm gonna take this

128

and throw it over my current because it

130.56

so i can do that

132.48

and then

133.68

if i jump into here and do source

136.8

current not sql

140.879

oh or exists because i need to do create

144.56

or replace

147.68

and let me fix that

149.76

here just so you can see it

152.48

because i was playing around with this

153.68

earlier so that's already there

156.959

so now we come here now if we source

159.2

okay he's good

160.879

so if i call raise signal

164.08

whoops if i spell it right

167.84

with the

168.84

one

170.56

then

171.36

nothing happens

173.2

if caught with a zero

174.959

oops that's not a one and it gives me

177.04

that error code

179.04

and so if we're looking here

181.92

and looking you know basically it's a if

183.68

so there's nothing there so if the value

185.68

is not equal to one

188.4

then it's going to give me this message

190.319

otherwise it does nothing

193.84

let's just do this let's do uh select

198.319

uh end of

200.4

program just to show you how this goes

206.959

oop then we'll do that but i do want to

209.12

do this

211.76

oh

212.56

let's go over here

214.72

just to

216.159

update my current

218

and now

221.519

if i source it

223.12

now if i call it with a zero

225.12

oops that is not one if i call it the

227.12

one

229.2

i get in a program so you can see here

231.12

that in this case

233.04

what happens is when i do that raise

236.879

i'm sorry that signal

239.519

when i signal

241.28

it does not give me

243.68

in the program it just says oops that's

245.76

not a one so signal bails out it

249.599

essentially raises an exception which is

252.64

this number and i can do let's call it

255.519

one two three

257.919

and let's go

259.44

where was my current let's change that

261.28

to like one two three

266.72

and let's uh source

271.36

signal with those zero one still works

273.36

fine if i sing with zero

275.52

then we can see that's

277.52

my return code

280

now the error

281.44

is it's a specific error because we've

283.84

raised this is something that sql is

285.36

going to give you but

286.8

because it gives you that id oops so

289.52

here it's a little bigger to see

291.919

because of the state it's going to

293.6

return that number so if you want to

295.36

have a custom error code

297.6

and

298.56

a message

299.84

then you can use signal

302

in order to do so

304.24

now

305.44

there are situations where

308.16

that could be a problem you don't want

309.759

to signal

311.68

and have something happen while you're

313.759

dealing with raising your exception

317.12

that gives you

318.639

that's where

320

resignal comes in

322.72

so in the resignal example

325.52

for this one we're going to have a

326.4

numerator and a denominator we're going

327.759

to do a division

330.24

and what we're going to do is we're

331.44

going to say if we see division by 0

336.84

then that's for if we hit this sql state

341.44

and we're going to declare our handler

343.12

which we've seen before so this is going

344.479

to be continue handler

346.96

and we're going to set

348.479

uh here

350.4

within this re-signal

353.84

we're going to set our

355.6

message

356.72

and the problem here is

361.919

is that when i come in here

364.08

if denominator equals zero and i do a

366.16

signal division by zero

369.84

then if i want to do anything within it

372.639

i have to essentially send it back up

374.8

otherwise

376.96

it's gonna it caused some

378.84

issues and that's why here i'm going to

381.6

get a signal but i also want to continue

384.08

it

385.52

so i'm

387.919

the flow is it's going to catch an error

390.319

and within that error

392

there is an exception but i'm basically

394.639

capturing that exception and so i need

396.8

to resign which means i need to pass

398.479

that back up the chain so rather than

400.72

just bailing out completely

402.96

because this is a continue

405.36

and because

406.479

i need to i want to

409.039

catch that exception if you think of

411.12

like a try catch i want to catch that

412.479

exception and then pass it up the chain

414.88

then i'm going to use a re-signal

416.639

instead

418.319

so in this case

422

let's see what oh i need to take this

423.68

let's make sure i get the whole thing

425.599

and throw that into my current

430.56

and then run that

435.36

uh results oops what did i do

443.44

oh i'm sorry in line

445.84

four

450.4

what did i do here uh

456

oh

459.36

declare results and

462.16

this is just going to be an ant

467.759

okay

471.039

i can change that my mistake missed one

473.199

in there

475.199

oh i need to group my creator replace

482.8

there we go so now i want to call it as

485.28

a resignal example with two numbers so

488.639

call resignal

491.36

example

493.12

and let's just do it uh two and one

496.16

how is that let's see numerator

497.84

denominator okay

499.84

so this should give us two over one

503.84

results two if we swap those two

507.52

should give us a half

509.36

uh oh it's giving us an int out my

512

mistake that should be a

514.24

float

517.76

now if we run current

521.76

there we go

523.039

and let's go back to the one we call it

524.56

two one and it's two so we're okay there

527.76

so we can

529.12

you know if we want to do i don't know

531.04

some number comma some smaller number

533.92

okay so we're gonna get our results so

535.36

that's working however

539.12

let's put let's start with a zero

542.48

on the numerator

543.839

and that's zero that's fine

547.12

but now if we do three comma zero

550.24

bam now we're getting the denominator

552.32

cannot be zero

554.8

so we have

556.24

uh in this oops so i need to look at it

559.2

so what we did is we came in here we

561.2

said hey i want to change the text

565.839

if otherwise let's see if i

570.48

get rid of that

572.959

i can't do it here

574.56

i get rid of that here

576.72

and get rid of my signal

581.04

when i run it uh set current

586.56

then

588.399

it's getting caught it's basically

590

getting just lost because i'm not

592.24

passing it back up

595.279

and so

596.48

uh this continue just says nope i'm not

598.24

going to do anything with it now if we

600.399

do uh let's do this

603.12

uh well

605.92

let's do

611.6

let's see what happens if we do select

613.44

result

614.72

regardless

619.92

in this case we do source

624.88

so we get denominator it cannot be zero

626.72

it bails out if we do it with a one

629.12

then it's going to come through and give

630.959

us our result

633.76

so we're still getting uh here because

636.399

it's a signal it was bailing out

639.12

before

641.44

um

642.959

so with this signal

646.24

we are rewriting the text if we don't

650.079

if we don't do it see remember if we

652.32

don't do it because this is a continue

653.839

handler

656.959

it just eats that example or that

661.6

there we go oh sorry re it eats that

664.24

exception

666.48

so it's going to come right through

667.44

because it's a continue handler now i

669.6

could say

671.2

if i just wanted to say

673.04

signal

677.36

then i'm going to get

680.64

here

686.959

i get that right

689.279

oh

696.16

then it's going to give me an issue

698

because

702.48

oh that's okay

705.36

because it's expecting that

707.36

signal that signal is always being

709.36

triggered and so it doesn't like that

714.48

so it's basically we're now just running

716.48

into a

718.8

syntax error

721.519

if we re-signal

727.36

then we're okay

729.2

and this is something we can play around

730.32

with a little bit as far as raising our

732.56

exceptions and dealing with them but

733.92

it's it's really and we'll see some more

735.839

of this as we get deeper into

738.24

uh some rather complex stored procedures

742.399

but this gives us a start so that we can

745.36

you know now between our prior

748.32

uh conversation when we talked about

751.12

exceptions in general and some of the

753.36

handlers whether it was a continue

754.88

handler or an exit

756.839

handler now we have the ability to also

761.04

signal specific errors which goes back

763.44

to here

764.35

[Music]

766.24

let's see

769.279

so we can do

770.959

exit handlers for specific

773.839

um or continue handlers for specific

776.8

errors

778.48

well

779.6

i can bring that up

781.12

and so actually i'll bring it over here

782.8

what's easier to read

784.24

where is that there you go

787.839

here then we can create our own

791.04

um

793.839

and we can set our signal state to a

795.6

specific spot we can start playing

797.2

around with that as well so we can

799.519

utilize

800.48

and

800.93

[Music]

802.48

basically

804

uh enhance or add to or even i mean just

807.44

i guess generally modify the

809.44

exception flow

811.04

we can use what's built in but we can

813.04

also change the messages we can

815.519

capture one do some work and then

817.12

continue on or capture it do some work

819.44

and then bail out

821.199

so it gives us

822.399

with these

823.519

this in the prior episode

826.32

we now have some tools

828.56

to do

829.44

very full-featured exception handling

832.079

which again

833.12

maybe it's not that big a deal

836.079

with the initial couple of store

837.519

procedures we've seen but fairly quickly

839.92

you'll see when you get into building

841.92

out stored procedures that there are

843.92

going to be situations where you're

845.279

going to want to bail out where you're

846.48

going to say hey i'm

847.92

i'm working on records that don't exist

851.12

or i'm spinning through a list of ids

853.44

and i'm looking things up based on that

855.36

and if one doesn't exist somewhere then

857.6

there's going to be an issue

859.36

it's actually could be for example a way

861.76

to

862.72

encode do

864.16

some sort of a foreign key relationship

866.16

as opposed to

867.839

doing it within

869.279

using the indexes and the keys

871.68

not sure why you would do that

874.079

but there are situations i think where

875.76

you would have something that is

879.519

depending on how tricky you want to get

880.72

it where you you're wanting to do some

882.16

sort of logic

883.44

based on

884.56

whether an id exists or not and it's not

888

really a foreign key because you're not

890

forcing it to exist but

892.399

you do want the ability to handle that

895.279

you want to have you know maybe the case

897.279

for example maybe you have a case where

898.72

if the foreign key doesn't exist

900.72

then you create that record uh you go

903.279

and create it link it up and then go

905.199

from there so there's

906.8

yeah i'm i'm sort of just throwing ideas

908.639

out at this point because once you get

909.92

into exceptions you're usually outside

911.839

of you're dealing with outliers anyways

914.399

even though they may be somewhat common

917.279

i guess outliers which

919.44

would make a little different but

921.92

you're basically saying hey here's a

923.68

couple of things that can happen i want

925.6

to be able to handle those conditions as

927.6

opposed to just

928.88

you know crashing

930.8

or

932

quietly doing nothing and then the user

934.16

has to figure out what's going on the

935.68

caller of that stored procedure has to

937.68

figure out what's going on this was a

940.32

little short but we've gone along the

942.32

last couple times so i figure we'll

944.399

we'll do one

946.16

and we will come back next time we're

947.759

just going to continue cranking our way

949.519

through

950.639

sql and learning how to use it better

953.519

but

955.04

go out there and have yourself a great

956.639

day

957.519

a great week and we will talk to you

960.72

next time

978.16

you