How to use a DECLARE statement

I'm relatively new to SQL and I'm still trying to learn how to set up a DECLARE statement.
For starters I don't really understand what the DECLARE is doing.. I understand select, and from what I believe DECLARE does is changes the field from one data output to a different data output.
I'm also brand new to this forum and from how I see it you guys write your example's different from how mine looks but I'm not sure exactly what everything means here.
So far this is how far I've gotten.
DECLARE my_table where my_column_name = ''

hi

declare zebra coloryellow

declare is like
saying this is something ( example a variable or cursor or etc etc )
what that something is ( example variable is of datatype integer or varchar etc etc)

example
declare @name varchar(100)

@ is used for variables
it is variable called @name
also its datatype is varchar(100)

DECLARE is used to create a variable - or declare the variable (including declaring a table variable). For example:

DECLARE @integerVariable int = 1;
DECLARE @stringVariable varchar(10) = 'var10';
DECLARE @table TABLE (col1 int, col2 int);

Once the variable has been declared - it can then be populated, modified and used in that batch.

SELECT ...
FROM dbo.myTable
WHERE myIntegerColumn = @integerVariable;

INSERT INTO @table VALUES (1, 2);

1 Like