From the course: Querying Microsoft SQL Server 2019

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Create and use variables in a query

Create and use variables in a query - SQL Server Tutorial

From the course: Querying Microsoft SQL Server 2019

Start my 1-month free trial

Create and use variables in a query

- [Instructor] The SQL language is primarily used for quarrying data out of a relational database but it can perform many other functions in the management of the database. Using SQL, you can create routines that function more like traditional programming languages by using variables and looping statements. Variables are placeholders that can be inserted into a query and assigned a value at a later point. To use one, you first need to create it and you do that using a DECLARE statement to initialize the variable. Variables are named using the @ symbol as their first character. You can then use whatever name you'd like. I'll call my MyFirstVariable. Next, you need to specify what type of data the variable will contain. Your options are the same data types that are used when creating fields in a SQL Server table. I'll set this variable into the int data type. That finished the DECLARE statement, so we'll end the line…

Contents