Home >Database >SQL >What is the command for variable declaration in sql

What is the command for variable declaration in sql

下次还敢
下次还敢Original
2024-05-07 05:36:16846browse

The command for variable declaration in SQL is DECLARE, which is used to declare variables to store temporary data. Syntax: DECLARE variable_name data_type [(size)] [DEFAULT default_value]. Parameters: variable_name is the variable name, data_type is the data type, (size) specifies the maximum length for the string type, [DEFAULT default_value] is the optional default value. For example: DECLARE @age INT; declares the integer variable @age. DECLARE @name VARCHAR(5

What is the command for variable declaration in sql

Commands for variable declaration in SQL

In SQL, use DECLARE command to declare variables. Variables are containers for storing temporary data and can be used in queries.

<code>DECLARE variable_name data_type [(size)] [DEFAULT default_value];</code>
Parameters

variable_name:
    The name of the variable.
  • data_type:
  • The data type of the variable, such as
  • INT , VARCHAR, DATE, etc. (size):
  • For string types, specifies the maximum length of the variable.
  • #[DEFAULT default_value]: Optional default value, used when the variable is not assigned a value
  • Example
Declare a name. For an integer variable of

@age:

<code>DECLARE @age INT;</code>
declare a 50-length string variable named

@name and set the default value to "Unknown":

<code>DECLARE @name VARCHAR(50) DEFAULT 'Unknown';</code>

Follow-up operations

After the variable is declared, you can use the

SET command to assign it

<code>SET @age = 25;</code>
It can also be used. Variables used in queries:

<code>SELECT * FROM customers WHERE age > @age;</code>

The above is the detailed content of What is the command for variable declaration in sql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn