560 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
How to create and use table variables in T-SQL
Part one of a three-part series of blogs
Table variables allow you to hold rows of data in temporary storage, without any processing overhead. Read this blog to add this technique to your SQL armoury!
I'll discuss later the pros and cons of table variables, but it's largely a matter of personal preference whether you use them instead of or as well as temporary tables. I like table variables because you never have to worry whether they already exist when you create them!
In a previous blog I showed that you can declare variables as follows:
where VariableType can be any of the standard data types in SQL. So the following are examples of variables:
DECLARE @LifeAnswer int = 42
DECLARE @AlternativeAnswer varchar(50) = 'Forty-two'
However, you can also declare a variable to refer to an entire table. The syntax for this is as follows:
DECLARE @TableName TABLE (
Why would you want to do this? Let's look at a worked example, showing how you could solve it with and without a table variable.
|Parts of this blog|
25 Aytoun Street