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