📺 Develpreneur YouTube Episode

Video + transcript

SQL Tutorial - Day 12

2022-05-12 •Youtube

Detailed Notes

1. Declare variables 2. Select into and returning a value; 3. Examples

delimiter // create or replace procedure tutorial.count_rows() BEGIN DECLARE num_rows INT DEFAULT 0;

END; // delimiter ;

delimiter // create or replace procedure tutorial.count_rows() BEGIN DECLARE num_rows INT DEFAULT 5;

select num_rows; END; // delimiter ;

delimiter // create or replace procedure tutorial.count_rows() BEGIN DECLARE num_rows INT DEFAULT 0;

select count(*) into num_rows from address;

select num_rows; END; // delimiter ;

delimiter // create or replace procedure tutorial.count_rows() BEGIN DECLARE num_rows INT DEFAULT 0; DECLARE a1,a2,a3,a4 INT DEFAULT 0;

select count(*) into a1 from address; select count(*) into a2 from app_user; select count(*) into a3 from lkp_type; select count(*) into a4 from all_dates;

SET num_rows = a1 + a2 + a3 + a4; select num_rows,a1,a2,a3,a4; END; // delimiter ;

delimiter // create or replace procedure tutorial.count_rows() BEGIN DECLARE num_rows INT DEFAULT 0; DECLARE a1,a2,a3,a4 INT DEFAULT 0;

select count(*) into a1 from address; select a1; select count(*) into a2 from app_user; select a2; select count(*) into a3 from lkp_type; select a3; select count(*) into a4 from all_dates; select a4;

SET num_rows = a1 + a2 + a3 + a4;

select num_rows; END; // delimiter ;

delimiter // create or replace procedure tutorial.cs_test(id int) BEGIN DECLARE c,s varchar(50) DEFAULT 'undefined';

select city,state into c,s from address where t_one_id = id;

select c,s; END; // delimiter ;

Transcript Text
[Music]
well hello and welcome back we are
continuing our my sequel
mostly sequel but my sequel maria db
focused tutorial series and we are
continuing looking at stored procedures
today now last time we went through and
we did a uh
back on day 11 we created our nice
little
uh tutorial get address where we could
send a parameter in
and pull a record back
also did a very simple one where we
showed some tables we did some things
where we did some selects
now we're going to get into something a
little different we're going to talk
about variables
now
i'll flip over here and i'm going to
dive into this one
so i do my normal
delimiter
let's blow this up a little more just
because
so i do my delimiter
and i'm gonna go ahead and create a
replace procedure i'm going to call this
one count rows
and in this one i'm going to declare num
rows
is an integer and you see this much like
you would most of the declarations in
the database so here i'm giving it a
name which is num rows
i'm getting it a type which is int
and then a default value which is zero
and then end so if i do this it's not
going to actually do
anything terribly useful
so if i do call get rows we're gonna see
basically
nothing oops
i'm sorry i get rows count rows
i should probably change my delimiter
back okay so nothing happens
because it wasn't doing anything
interesting all it was doing was
declaring a variable now what i can do
here
same thing so i'm going to declare it
and now i'm going to select
rows
let's go ahead and give it a default
value of like five
and now
actually let's do this
just so we can keep track of these guys
that way you've got some good examples
okay so
now we're going to create a replace this
time we're going to set our value to
five and we're going to do a select
number oops we're going to do that just
to be clean
i don't think it likes it otherwise
and now if we come in
and we do our count rows wherever that
went
is that right
uh count rows now
we see num rows
is five so we're getting a value back
that means that we can come in with our
variables and we can do some interesting
stuff like
let's say num rows is 4 but instead and
we're going to still return gnome rows
but instead
we're going to come in here and we're
going to do
select
account star from
let's say address
up and we're going to do select into
num rows count star from address
i'm sorry wait it's count start into
let's forget the order of that
so now
let's see what that does
ah
there we go
so now
uh where'd it go call call count rows
now it's three and if you look at select
star from
address
you see three rows
so now we're actually able to do
something here now let's do this let's
just show tables
and let's do
well i just gotta copy all of those so
here's what we're gonna do
is
now we're gonna get a little more
interesting let's do this
so we're gonna take a few tables
uh let's see we're going to call it ah
we'll keep it the same name
but now
we're going to do address
uh let's do
amp user
lookup type
and
all dates
and now
we're going to change this up a little
bit
so in each of these we're selecting
account
now here
we can do num rows
but we're going to do
now we're gonna do a multiple so we're
gonna do
a one a two a three and a four for our
four counts
so we can declare multiple variables
and we're still declaring the same type
and the same default value for each of
these so now instead of num rows
i do a
one a2
a3 oh
a3
when i get there
a4
so now we've got all these so instead of
select num rows what we want to do is
now we want
rows equals
a1 plus a2 plus a3 plus a4
and with this we do a set
that's just how they do it so you can't
do a straight up num rose equals you do
a set
now we're going to select num rows
and let's see how this works out
i have no idea how many rows it's going
to be but it's probably like a double
digit number
so now we call
oh
count rows
we get 13.
so we're getting each of those now we
could come in and we could do
select a1
we could select as we go so we can
actually get all of these as we go and
see how it's going
three and a four
so let's do this with sort of a debug
and i'm just going to do what was that
count
rose
it's probably faster to do it that way
at least if i do it like this
and so now we can see
that our a1 came through we got 3 for
that 4 for a2 3 for a3 and 3 for a4
which is 3 plus 4 is 7 plus 3 is 10 plus
3 is 13 and boom we have our total
number of rows
so if we've been able to
declare variables
and
do something with them which you could
normally do you can do some of this
we haven't gotten into it but you can do
it with your normal script type of
information
we're just doing this with now within a
sort procedure so we can actually do
something we can actually hold some
records and do
something with them
we can actually
do some manipulation now within it so we
could do things like
uh let's see
what would be something interesting i
have no idea of something nervous to do
so we're gonna skip that part about the
interesting part but okay sorry back to
this
so we're able to
when we do a select without an n2
as we see here in the a1 a2 a3 a4
and then the final one
we can see that we're going to see this
we see this output
this stuff is actually sent out as part
of the result
now this doesn't it's not how it occurs
in all
databases this is what saying sort of
important is that some you can have
multiple result sets come back some will
only allow one
so you're going to have to look at your
specific database if you're getting away
from these two
but if i get rid of my selects
and let me go back to that
then all of this other stuff
if i get rid of the selects that aren't
an into
then
i don't see the declare don't see that
one i don't see any of these selects i
don't see setting the gnome rows i don't
see it until i do select num rows
now i could do num rows
comma a1 comma a2 comma a3 comma a4
[Music]
so i can do that
and give myself a little something
different from my result set
and now we see
num rows plus i see each of the
variables
so you can see where i specifically set
them
otherwise the and this becomes
important when you get into the
development side of it because this is a
single result set
the time we did it before what we did
before was multiple result sets it was
one two three four five result sets
so you're gonna have to depending on
what your
your connection is and how it handles
multiple result sets you're gonna have
to address those differently than you
would this one where this is simply row
one column one column two come three
comma four column five
there's different ways that you're going
to address such a thing
now
you can do
uh let's do a quick let's do like let's
select star from
address just to see what that looks like
again so let's do city state uh yeah
let's do city state
and we can do
let's jump down here
we're just going to call it cs test
and in this case we don't need to
declare well we will declare this
but now
um
we're just going to make these march our
50s
i can't remember how big our
city was
but we can do city and state
and now
we can select city
state
into c comma s from address
and let's see what this looks like
when we get done and we do
select cs uh let's default those can't
default to that let's default it to
undefined
and let's see what it does for us
oh
darn it
i want to get that
and okay now it looks good
so now let's see what happens when i do
cs tests
result consists of more than one row now
this is where
i'm getting multiple because i just
i just did this into that from address
well since it's in the c and s
from address
c is a single
string s is a single string but address
is going to select if i actually do
select city state
from address remember we're going to get
multiple
records back
so i have to do something to
in this case if i want just one of those
then it would be something like uh
let's see what was the address t1id then
i could come back
and i can do int
[Music]
id
where what did i call that uh
t1id
equals id
and now
i'm gonna have to give it
what uh i do
replace that
uh
let's see
so into id
let's look at that again
let's make sure i've got this
and
i didn't miss anything weird did i oh
i'm sorry it's the opposite direction
i forgot it is
i do that wrong half the time it's not
its name and then type
okay so now if i do call what is that cs
test
and i give it a value that's very that's
valid
for one of those three ids let me get a
record if i give it one that's not
then i'm going to get nulls back because
that now i see that default value come
in it did not get a record back for this
so it didn't assign those in
and so instead it just went with their
default values
so
[Music]
we can play around with this quite a bit
and
are going to continue to do so we'll
look at some things like
actually building with our result sets
and some things like that so we're going
to get a little more complicated but i
want to get into just some basics this
time around
of declaring variables
and assigning things to them you can
either do a select into
you know select some value
into our variable or we can do a set we
can set our value equal to
another variable or about like a number
because we could always do
set num rows
equals 15. we can do the same thing
there
we just did it as a mathematical
expression before so we've got a couple
different ways to
play around with this already you should
start seeing some of the power of a
stored procedure
particularly if you're having to do some
complex calculations and things like
that where you may want to
like hold a max or some calculation or
sum from one table and then utilize that
into another
and like i said we're going to continue
looking at that but for now it gets a
good start into our introduction to
variables in sql
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

well hello and welcome back we are

29.359

continuing our my sequel

31.84

mostly sequel but my sequel maria db

34.239

focused tutorial series and we are

37.28

continuing looking at stored procedures

39.44

today now last time we went through and

42.96

we did a uh

45.52

back on day 11 we created our nice

47.76

little

49.12

uh tutorial get address where we could

51.039

send a parameter in

52.719

and pull a record back

55.039

also did a very simple one where we

56.48

showed some tables we did some things

58.16

where we did some selects

60.719

now we're going to get into something a

61.92

little different we're going to talk

62.8

about variables

65.84

now

67.439

i'll flip over here and i'm going to

69.28

dive into this one

72.479

so i do my normal

74

delimiter

75.68

let's blow this up a little more just

78.56

because

81.52

so i do my delimiter

83.2

and i'm gonna go ahead and create a

84.24

replace procedure i'm going to call this

85.439

one count rows

87.439

and in this one i'm going to declare num

89.84

rows

90.799

is an integer and you see this much like

93.119

you would most of the declarations in

95.04

the database so here i'm giving it a

97.28

name which is num rows

99.439

i'm getting it a type which is int

102.32

and then a default value which is zero

104.64

and then end so if i do this it's not

107.119

going to actually do

109.119

anything terribly useful

114.479

so if i do call get rows we're gonna see

117.36

basically

118.479

nothing oops

123.6

i'm sorry i get rows count rows

129.52

i should probably change my delimiter

131.12

back okay so nothing happens

134.48

because it wasn't doing anything

137.04

interesting all it was doing was

138.319

declaring a variable now what i can do

140

here

141.28

same thing so i'm going to declare it

142.8

and now i'm going to select

146.64

rows

147.76

let's go ahead and give it a default

148.8

value of like five

152.56

and now

153.92

actually let's do this

156.64

just so we can keep track of these guys

164.959

that way you've got some good examples

166.48

okay so

168.8

now we're going to create a replace this

170.8

time we're going to set our value to

172.239

five and we're going to do a select

173.519

number oops we're going to do that just

175.44

to be clean

177.36

i don't think it likes it otherwise

179.92

and now if we come in

182.239

and we do our count rows wherever that

186.239

went

187.84

is that right

190

uh count rows now

192.64

we see num rows

194.48

is five so we're getting a value back

197.36

that means that we can come in with our

201.36

variables and we can do some interesting

203.44

stuff like

205.519

let's say num rows is 4 but instead and

209.599

we're going to still return gnome rows

212.4

but instead

214.64

we're going to come in here and we're

215.519

going to do

216.4

select

218.159

account star from

220.879

let's say address

223.36

up and we're going to do select into

228.4

num rows count star from address

233.2

i'm sorry wait it's count start into

237.68

let's forget the order of that

242.239

so now

243.36

let's see what that does

248.239

ah

249.2

there we go

253.04

so now

259.12

uh where'd it go call call count rows

262.32

now it's three and if you look at select

264.72

star from

266.84

address

268.72

you see three rows

270.56

so now we're actually able to do

272.16

something here now let's do this let's

273.68

just show tables

276.8

and let's do

278.72

well i just gotta copy all of those so

280.72

here's what we're gonna do

283.28

is

285.84

now we're gonna get a little more

286.8

interesting let's do this

289.28

so we're gonna take a few tables

293.68

uh let's see we're going to call it ah

297.28

we'll keep it the same name

301.44

but now

305.44

we're going to do address

309.12

uh let's do

313.12

amp user

315.199

lookup type

319.68

and

321.44

all dates

325.039

and now

326.24

we're going to change this up a little

327.28

bit

328.08

so in each of these we're selecting

329.52

account

330.88

now here

332.479

we can do num rows

334.479

but we're going to do

336.639

now we're gonna do a multiple so we're

338

gonna do

340.8

a one a two a three and a four for our

344.72

four counts

348.32

so we can declare multiple variables

355.6

and we're still declaring the same type

357.84

and the same default value for each of

359.68

these so now instead of num rows

363.36

i do a

364.84

one a2

368.84

a3 oh

370.96

a3

372.4

when i get there

375.68

a4

380.319

so now we've got all these so instead of

382

select num rows what we want to do is

384

now we want

385.44

rows equals

387.52

a1 plus a2 plus a3 plus a4

393.68

and with this we do a set

396.72

that's just how they do it so you can't

398.16

do a straight up num rose equals you do

399.759

a set

400.8

now we're going to select num rows

402.72

and let's see how this works out

406.72

i have no idea how many rows it's going

408.319

to be but it's probably like a double

409.759

digit number

414.4

so now we call

419.919

oh

421.44

count rows

422.72

we get 13.

424.8

so we're getting each of those now we

426.56

could come in and we could do

430.24

select a1

433.12

we could select as we go so we can

434.88

actually get all of these as we go and

436.88

see how it's going

439.199

three and a four

441.759

so let's do this with sort of a debug

448.56

and i'm just going to do what was that

449.919

count

451.36

rose

452.88

it's probably faster to do it that way

456.4

at least if i do it like this

459.919

and so now we can see

461.599

that our a1 came through we got 3 for

464

that 4 for a2 3 for a3 and 3 for a4

467.68

which is 3 plus 4 is 7 plus 3 is 10 plus

470

3 is 13 and boom we have our total

472.879

number of rows

475.44

so if we've been able to

477.52

declare variables

479.28

and

480.56

do something with them which you could

482.639

normally do you can do some of this

484.8

we haven't gotten into it but you can do

486.08

it with your normal script type of

488

information

489.28

we're just doing this with now within a

491.599

sort procedure so we can actually do

494

something we can actually hold some

496.08

records and do

498.319

something with them

500

we can actually

501.84

do some manipulation now within it so we

503.919

could do things like

506.96

uh let's see

512

what would be something interesting i

513.599

have no idea of something nervous to do

514.959

so we're gonna skip that part about the

516.32

interesting part but okay sorry back to

518.64

this

520.8

so we're able to

522.8

when we do a select without an n2

526.08

as we see here in the a1 a2 a3 a4

529.76

and then the final one

532.08

we can see that we're going to see this

534.32

we see this output

535.839

this stuff is actually sent out as part

537.76

of the result

540

now this doesn't it's not how it occurs

542.399

in all

543.68

databases this is what saying sort of

546

important is that some you can have

548.08

multiple result sets come back some will

550.08

only allow one

551.92

so you're going to have to look at your

553.36

specific database if you're getting away

555.04

from these two

557.519

but if i get rid of my selects

559.68

and let me go back to that

564

then all of this other stuff

567.519

if i get rid of the selects that aren't

569.68

an into

573.2

then

574.16

i don't see the declare don't see that

575.68

one i don't see any of these selects i

577.44

don't see setting the gnome rows i don't

579.279

see it until i do select num rows

582.08

now i could do num rows

584

comma a1 comma a2 comma a3 comma a4

589.1

[Music]

590.72

so i can do that

593.04

and give myself a little something

594.56

different from my result set

600.64

and now we see

602.24

num rows plus i see each of the

604.16

variables

606.399

so you can see where i specifically set

608.48

them

609.36

otherwise the and this becomes

611.76

important when you get into the

612.88

development side of it because this is a

615.44

single result set

617.04

the time we did it before what we did

618.959

before was multiple result sets it was

621.279

one two three four five result sets

623.76

so you're gonna have to depending on

625.279

what your

626.8

your connection is and how it handles

629.839

multiple result sets you're gonna have

632.959

to address those differently than you

634.8

would this one where this is simply row

637.76

one column one column two come three

639.519

comma four column five

642.48

there's different ways that you're going

644

to address such a thing

646.399

now

648

you can do

649.6

uh let's do a quick let's do like let's

651.839

select star from

655.76

address just to see what that looks like

657.36

again so let's do city state uh yeah

660.48

let's do city state

662.72

and we can do

669.36

let's jump down here

675.04

we're just going to call it cs test

678.8

and in this case we don't need to

680.399

declare well we will declare this

683.36

but now

685.12

um

686.079

we're just going to make these march our

688.48

50s

689.6

i can't remember how big our

691.76

city was

694.64

but we can do city and state

697.839

and now

699.839

we can select city

702.399

state

704.399

into c comma s from address

709.92

and let's see what this looks like

711.839

when we get done and we do

715.36

select cs uh let's default those can't

717.92

default to that let's default it to

719.839

undefined

724.24

and let's see what it does for us

726.88

oh

728.24

darn it

729.36

i want to get that

732.44

and okay now it looks good

738.24

so now let's see what happens when i do

740

cs tests

746.72

result consists of more than one row now

748.639

this is where

751.279

i'm getting multiple because i just

753.76

i just did this into that from address

756.079

well since it's in the c and s

758.56

from address

761.12

c is a single

762.72

string s is a single string but address

764.959

is going to select if i actually do

766.72

select city state

769.519

from address remember we're going to get

771.04

multiple

772.8

records back

774.48

so i have to do something to

776.72

in this case if i want just one of those

779.36

then it would be something like uh

782.32

let's see what was the address t1id then

785.519

i could come back

786.88

and i can do int

788.41

[Music]

789.6

id

792.48

where what did i call that uh

795.2

t1id

798.56

equals id

801.279

and now

804.32

i'm gonna have to give it

813.44

what uh i do

820.72

replace that

828.32

uh

829.519

let's see

830.88

so into id

837.92

let's look at that again

839.68

let's make sure i've got this

844

and

848.959

i didn't miss anything weird did i oh

851.279

i'm sorry it's the opposite direction

853.68

i forgot it is

856.72

i do that wrong half the time it's not

859.12

its name and then type

863.92

okay so now if i do call what is that cs

867.199

test

868.32

and i give it a value that's very that's

870.32

valid

872

for one of those three ids let me get a

874.399

record if i give it one that's not

877.04

then i'm going to get nulls back because

878.72

that now i see that default value come

881.04

in it did not get a record back for this

883.76

so it didn't assign those in

885.76

and so instead it just went with their

887.12

default values

890

so

890.33

[Music]

892.16

we can play around with this quite a bit

894.88

and

895.68

are going to continue to do so we'll

898.24

look at some things like

899.68

actually building with our result sets

901.199

and some things like that so we're going

902.32

to get a little more complicated but i

903.68

want to get into just some basics this

905.519

time around

906.639

of declaring variables

908.639

and assigning things to them you can

910.959

either do a select into

912.959

you know select some value

914.88

into our variable or we can do a set we

918.32

can set our value equal to

920.72

another variable or about like a number

922.959

because we could always do

925.68

set num rows

929.04

equals 15. we can do the same thing

931.199

there

933.759

we just did it as a mathematical

935.519

expression before so we've got a couple

937.68

different ways to

939.12

play around with this already you should

941.12

start seeing some of the power of a

942.56

stored procedure

944.24

particularly if you're having to do some

946.16

complex calculations and things like

948

that where you may want to

950.24

like hold a max or some calculation or

953.279

sum from one table and then utilize that

955.759

into another

958

and like i said we're going to continue

959.519

looking at that but for now it gets a

961.519

good start into our introduction to

963.839

variables in sql

966.56

that being said we'll wrap this one up

968.399

so go out there and have yourself a

969.839

great day a great week and we will talk

972.56

to you

973.839

next time

990.959

you