From the course: Hands-On Advanced SQL Server: Strategies and Techniques
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Variables - SQL Server Tutorial
From the course: Hands-On Advanced SQL Server: Strategies and Techniques
Variables
- [Instructor] So far you've learned how to make your stored procedures more flexible by passing in parameters, but what if we wanted to store values inside a procedure for calculations, conditional logic, or to reuse later in the script? Now, that's where variables come in. A variable in SQL Server is like a temporary storage container. You give it a name, assign a value, and you can use it throughout your procedure or batch. Think of it kind of like a notepad inside your script. You jot down a value once and reference it as many times as you need to. All right, so let's take a look at declaring and using a variable here. Now, let's say we want to store a minimum runtime value in a variable before using it in a query. This is a statement that does it for us. First, we declare min length int, which creates a variable called min length that holds integers. Next, we use the set keyword to set the min length to 120. This actually assigns this value to it. Then we use the min length in…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.