Constants and variables in SQL Server and judgment loop statements

Source: Internet
Author: User
Variables in the database and variables defined in the constant database [Act as a temporary data storage function] --- define variables in the database (run from start to end during runtime, from defining variables to assigning values) -- declare must be added before -- Define the variable: declare @ variable name data type declare @ aavarchar (20) -- assign the value set @ aa

Variables in the database and variables defined in the constant database [Act as a temporary data storage function] --- define variables in the database (run from start to end during runtime, from defining variables to assigning values) -- declare must be added before -- Define the variable: declare @ variable name data type declare @ aa varchar (20) -- assign a value to set @ aa

Database variables and constants

Define variables in the database

[Function of temporary data storage]

--- Define variables in the database (run from start to end during running, from defining variables to assigning values)

-- Declare must be added before

-- Define variable: declare @ variable name Data Type

Declare @ aa varchar (20)

-- Value set @ aa = 'hello'

Set @ aa = 'wusong'

Select * from xuesheng where name = @ aa

-- You can also assign values after select.

Select @ aa = Name from xuesheng where student ID = 10

Applications of variables include:

System constant

-- Returns the number of connections since SQL Server was last started, regardless of whether the connection succeeds or fails.

Print @ connections

-- Returns the error when the previous SQL statement is executed. If the returned result is correct, it indicates that there is an error.

Print @ error

-- Returns the current language.

Print @ language

-- Number of rows affected by the previous sentence (when performing the modification operation, check the number of rows modified)

Print @ rowcount

-- Returns the version information of the database to be installed.

Print @ version

Judgment and loop statements

[In the use of database judgment loop statements, there is no braces used, instead of begin end, the code block is written between begin end]

For example:

Basic If statement format

Basic While statement format

Example: Accumulate and sum

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.