SQL Server series: Transact-SQL variables

Source: Internet
Author: User

A variable is a user-defined, assignable, and participating entity in Transact-SQL, which can be divided into global variables and local variables in two categories. Where global variables are customized and maintained by the system, global variable names are signed with @@ 字符, and any program can be called at any time. The local variable name is preceded by an @, which is defined and used by the user.

1. Local Variables

Local variables typically appear in batches, stored procedures, and triggers, and if you want to use local variables, you must define them in the Declare statement before use. A local variable exists only in the batch, stored procedure, or trigger that declares it, and the information stored in the local variable is lost after processing ends.

1.1 Local variable Declaration

When you use the DECLARE statement to declare a local variable, you must supply the variable name and data type, which must precede the name of the @ character, and the maximum length of the variable name is 30 characters. You can use any system-defined data type or user-defined data type when declaring a variable.

DECLARE syntax:

DECLARE @variable_name | [= value] [,... n]

Parameters:

◊ @variable_name: The name of the variable. Variable names must begin with @.

◊datatype: Any system-provided or user-defined data type. The variable data type cannot be text, ntext, or image.

In SQL Server, you can define more than one variable at a time.

DECLARE @Name VARCHAR (@AgeINT=

SQL Server series: Transact-SQL variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.