declare

SQL Variable Declaration - How to Declare a Variable in SQL Server - SQL Training Online



Sharing buttons:

okay joy blue here and today I want to

talk about the sequel variable

declaration so every variable in sequel

server starts with an @ sign and so in

order to use it well here let me show

you that so if I went to variable I

could give it a name and I start with

the @ sign and I would say my variable

right now in order to declare it or

actually create the variable you have to

start with declare just like that so now

I have a variable but I left something

off I need to give it a type so a type

can be any of the data types that you

can put in a column for sequel server

which would be like a integer or a

decimal or a varchar' or a a date/time

or whatever right so we'll start and say

declare we're going to make an integer

and I will just say my int make it

descriptive and so once you've declared

an integer now you have that variable

but you need to put something in it

right and so I've declared it as as a

integer and now to put something in I

can either use set or select and now

we'll use set right here so I'll say set

at my int and I could say equal to 3

right and so now it'll have something in

it the number 3 and if I want to see

what's in it all I have to do is say

select at my int and you can see

intellisense actually shows you that it

knows about this my hand right and so

let's run those three things here and

there you have it so 3 was in there now

I could go on and say set at my int

equal to at my int plus 1 so we declared

it as an integer we set it equal to 3 so

now 3 is inside of it and then we took

the 3 add it 1 to it and put it inside

of it so we should get a 4 so let's go

ahead and see if that's what we get here

run that and we've got a 4 so I did tell

you that there's another way to set this

and the other way you can set it is to

say select like that let's see if that

works also and so you can see that works

also seeing you set or select as always

if you find this helpful please leave a

comment below click the like button or

subscribe to my channel you can also

visit me a sequel training online.com

and I hope you enjoyed this free video

you