Declare variables in SQL Server
SQL Server fully exists the concepts of data types, variables and declarations of variables as other programming languages. The article will learn how to declare a variable, multiple variables and assign default values to variables in SQL Server. Invites you to read the track.
Variable (Variable) used to store temporary values during the execution of the algorithm.
Syntax of variable declaration in SQL Server
To declare variables in SQL Server, we use the DECLARE statement, expressed as follows:
DECLARE @variable_name datatype [ = initial_value ],
@variable_name datatype [ = initial_value ],
.;
Parameters:
- variable_name : name assigned to the variable.
- datatype: data type of variable.
- initial_value: default value assigned to variable (optional).
Declare a variable in SQL Server
Use DECLARE to declare any variable
DECLARE @quantrimang VARCHAR(50);
This DECLARE statement declares a variable named @quantrimang, with VARCHAR data type and a length of 50 characters.
You then change the value of the @quantrimang variable using the SET statement.
SET @quantrimang = 'Hello world';
Next try the INT data type:
DECLARE @site_value INT;
Use the SET statement to assign values to the @site_value variable
SET @site_value = 10;
So the @site_value variable here is assigned to the integer 10.
Declare many variables in SQL Server
How to use the following command:
DECLARE @quantrimang VARCHAR(50),
@site_value INT;
In this example, we have two variables declared: @quantrimang variable with VARCHAR (50) data type and variable @site_value INT data type.
Declare the variable with the default value in SQL Server
In SQL Server, we can assign the default value to the variable at the time of declaration.
DECLARE @quantrimang VARCHAR(50) = 'Hello world';
So here the @quantrimang variable with VARCHAR data type and the length of 50 characters are assigned by default to the 'Hello world' value.
Similarly, we declare with the INT data type:
DECLARE @site_value INT = 10;
Declare more than one variable with the initial value assigned
How to use the following command:
DECLARE @quantrimang VARCHAR(50) = 'Hello world';
@site_value INT = 10;
The two variables @quantrimang and @site_value have been declared at the same command and assigned the default initial values.
Previous lesson: LITERAL (Hang) in SQL Server
Next post: SEQUENCE in SQL Server
You should read it
May be interested
- Instructions for installing Unix / Linuxan important unix concept is the environment, which is defined by environment variables. some are set up by the system, others are set up by you, by the shell or any program you download.
- Network basics: Part 3 - DNS Servera dns server is a server that contains a database of public ip addresses and hostnames associated with them. in most cases, the dns server is used to resolve or translate those common names into ip addresses as required.
- Array in Language Cthe c programming language provides a data structure called an array, stored in a set of data of the same type with fixed length. an array is used to store data sets, but it is useful if you think of an array of variables with the same type.
- PHP for Programmer Cthe easiest way to think of php is as c is interpreted that you can embed in html documents. php is quite similar to c, except for untyped variables, web-specific libraries are available, and everything is hooked up directly to your favorite web server.
- Use variables in Shella variable is a string of characters from which we assign a value. the assigned value can be a number, text, file name, device or any other type of data.
- Scope rules in programming Ca scope in any program is an area in the program where a defined variable exists and outside of that range the variable cannot be accessed.
- How to set up your own Git server on Linuxwhile you can count on globally renowned git hosting services like github, in some cases it is better to host a personal git server for enhanced privacy, customizability, and security.
- How to Write PHP Scriptsphp is a server scripting language used to make web pages interactive. it became widely popular due to its ease of use, interactivity within web pages, and integration with html. think of what happens when a page is edited on this website....
- stddef.h in Cthe file header named stddef.h in c library defines various types of variables and macros. many of these definitions are also present in other headers.
- Use IIS to set up FTP Server on Windowsset up an ftp server (file transfer protocol server) to share and convert large files with unlimited traffic.